-1

The following is a part of my code. I have a function that scans the com ports and pulls the available com ports that are connected. I want to list these com ports as different selections in an Option Menu. When I run this code, the only option available is COM2 COM4, is there a simple way to separate these into two different options? I am not looking only for how to populate the option menu with a list, but how to convert the data I have to a list. Thanks.

    global model
    model = StringVar(master)
    self.option = StringVar()
    w = OptionMenu(master, self.option, ())

From the scan of com ports, the print statement returns ['COM2', 'COM4'].

    print(serial_ports())
    global com
    com = list(serial_ports())
    self.option.set(com)

Below is the full code for scanning the com port.

    def serial_ports():
        """ Lists serial port names

            :raises EnvironmentError:
                On unsupported or unknown platforms
            :returns:
                A list of the serial ports available on the system
        """
        if sys.platform.startswith('win'):
            ports = ['COM%s' % (i + 1) for i in range(256)]
        elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
            # this excludes your current terminal "/dev/tty"
            ports = glob.glob('/dev/tty[A-Za-z]*')
        elif sys.platform.startswith('darwin'):
            ports = glob.glob('/dev/tty.*')
        else:
            raise EnvironmentError('Unsupported platform')

        result = []
        for port in ports:
            try:
                s = serial.Serial(port)
                s.close()
                result.append(port)
            except (OSError, serial.SerialException):
                pass
        return result

    if __name__ == '__main__':
        print(serial_ports())
        global com
        com = serial_ports()

        self.option.set(com)
Joe.M
  • 21
  • 4
  • Possible duplicate of [How do populate a Tkinter optionMenu with items in a list](https://stackoverflow.com/questions/18212645/how-do-populate-a-tkinter-optionmenu-with-items-in-a-list) – Ethan Field Sep 12 '17 at 14:39
  • This does not solve my question, I need to know how to convert what I have into a list that can populate the optionmenu. Th link above assumes you already have a list. I tried a couple of ways to make it into a list, with no success so far. Thanks – Joe.M Sep 12 '17 at 14:43
  • So `['COM2', 'COM4']` which follows the conventions of a Pythonic list is not a list? Have you tried iterating over it? – Ethan Field Sep 12 '17 at 14:49
  • ['COM2', 'COM4'] is what prints in the above print statement. I have tried just setting the option to that and also using the list() and then setting the optionmenu. In both cases, only one option is available which is COM2 COM4. Thanks – Joe.M Sep 12 '17 at 14:52
  • If you iterate over the output of the print statement what happens? – Ethan Field Sep 12 '17 at 14:55
  • Hi Joe. The returned print statement is a list. So `serial_ports()` is creating a list already so `com = serial_ports()` should be the same as `com = ['COM2', 'COM4']`. However I think you should show more of your code because I can see you are using global and self in the same code and this tells me that you are using global variables inside of a class. You should try to avoid this. – Mike - SMT Sep 12 '17 at 14:56
  • I have edited and entered the full code of scanning the com port. This was taken from an example found on line and seems to work. Now I am just trying to take the output of this and put it into an option menu for the user to select the proper com port when starting a program. Thanks. – Joe.M Sep 12 '17 at 15:08
  • `com` is already a list because `result` is a list and you are returning `result`, if you follow the instructions of my previously linked question it should work as you need it. – Ethan Field Sep 12 '17 at 15:22
  • @EthanField My bad I posted an answer to another question i had open. – Mike - SMT Sep 12 '17 at 15:37
  • @SierraMountainTech We've all done it, I just wanted to know what I was missing :D – Ethan Field Sep 12 '17 at 15:39
  • Ethan, I have tried just setting the option menu to com, but it only populates the first option as COM2 COM4. There is no second option available. Thanks – Joe.M Sep 12 '17 at 15:45
  • Did you read the linked question, because it does not say to just call the list object as it is. – Ethan Field Sep 12 '17 at 15:49
  • Thank you for your assistance. I will need to read up on how this is done and figure it out. Thanks for trying to help me. – Joe.M Sep 12 '17 at 15:59
  • Try using `*com`. – Ethan Field Sep 12 '17 at 15:59
  • Every way I try it, it seems to either COM2 COM4 as the only selection or just PY_VAR1 as the only selection. Thanks for the suggestion. – Joe.M Sep 12 '17 at 17:20

1 Answers1

0

This proves that what I have said works:

from tkinter import *

root = Tk()

var = StringVar()
array = ["COM2", "COM4"]
var.set(array[0])

OptionMenu(root, var, *array).pack()

root.mainloop()

Please read this:

https://stackoverflow.com/a/18213202/4528269

Ethan Field
  • 4,646
  • 3
  • 22
  • 43