0

The variable ports_available contains an array of COMPORTS. I want these to be added to the submenu "ports_menu" on startup of the GUI so that I can select any one of them for selecting the COMPORT.

global ports_available
ports_available = [comport.device for comport in serial.tools.list_ports.comports()]

global port_select
port_select = "COM1"

#Create Menu
my_menu = Menu(window)
window.config(menu = my_menu)

# Add Tool Menu
tool_menu = Menu(my_menu, tearoff = False)
my_menu.add_cascade(label = "Tools", menu = tool_menu)
tool_menu.add_command(label = "Bold", command =bold_it)
tool_menu.add_command(label = "Italic", command =italic_it)

#Menu for ports

ports_menu = Menu(tool_menu, tearoff = False)
tool_menu.add_cascade(label = "Ports", menu = ports_menu)

for name in ports_available:

    ports_menu.add_checkbutton(label = name, variable = port_select)

[![When i select any one of the option everything gets selected][1]][1]

Thank you, Dhruv Gupta

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • You can get the answer to this by reading existing documentation. It's not clear why you need our help. Please [edit] your question to include a [mcve] which illustrates the problem you're having. – Bryan Oakley Nov 10 '20 at 15:46
  • I read the documentation and everywhere add_cascade command is showing. I have a list which is changing every time i start the tkinter app and accordingly the submenu should be updated. – dhruvguptadg Nov 10 '20 at 15:50
  • What is stopping you from updating the submenu each time it starts? Please show what you've tried. – Bryan Oakley Nov 10 '20 at 15:54
  • I have updated the question, requesting you to have a look at it. Thanks. – dhruvguptadg Nov 10 '20 at 16:52
  • I went to this [solution](https://stackoverflow.com/questions/20429448/tkinter-how-to-create-submenus-in-menubar) which you posted long back. In that I have added the list to the submenu but how to access those values separately. I have three different ports in the list and want to select any one from it and load the value into a variable. – dhruvguptadg Nov 10 '20 at 17:11
  • Why aren't you simply iterating over `ports_available` to add items to the menu? – Bryan Oakley Nov 10 '20 at 17:12
  • I did that i guess and edited the code. But what should i do to store only one of the value from the options when i select it from the sub-menu ? – dhruvguptadg Nov 10 '20 at 17:15
  • Are you wanting a command, or do you want to set the value of a variable? Both of those are also documented. Are you aware you can add radiobuttons and checkbuttons to menus? – Bryan Oakley Nov 10 '20 at 17:27
  • I want to set the value of the variable. No, I don't know about the radio buttons or check buttons in menus. – dhruvguptadg Nov 10 '20 at 17:37
  • They are all documented. Look for `add_checkbutton` and `add_radiobutton`. – Bryan Oakley Nov 10 '20 at 17:58
  • Thank you so much @BryanOakley the radio buttons were really useful. It worked how i wanted it to. – dhruvguptadg Nov 10 '20 at 18:27

1 Answers1

0

Based on the comments you posted, what you're wanting is to set a variable based on a menu selection. You can add radiobuttons to a menu with add_radiobutton. The use of radiobuttons will allow the user to make an exclusive choice.

Here's an example:

import tkinter as tk

ports = ["COM1", "COM2", "COM3"]

def show_value():
    print(f"selected port: {port_selection.get()}")

root = tk.Tk()

port_selection = tk.StringVar(value=ports[0])
menubar = tk.Menu(root)
port_menu = tk.Menu(menubar)
for port in ports:
    port_menu.add_radiobutton(label=port, value=port, variable=port_selection, command=show_value)

root.configure(menu=menubar)
menubar.add_cascade(label="Port", menu=port_menu)

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685