pythontkinterdrop-down-menudynamic-list

Tkinter dynamically changing option menus


I'm creating an application with a tkinter-based GUI where a user can manually or automatically control a set of 16 solenoid valves with raspberry pi GPIO pins.

I want there to be an option where the user can reconfigure which valve is assigned to which GPIO pin. Currently I just have them load a text file with the pin numbers in the order they want on a single line, separated by spaces (e.g. 13 16 40...).

What I'd like is for the user to open a window where there is a tkinter option menu for each valve, and they select which GPIO pin they want the valve assigned to. The challenge I'm facing is finding out how to have all those option menus linked to the same list but not allowing the user to assign two or more valves to the same pin. For example, once I select valve 1 to be linked to pin 40, the rest of the option menus would no longer show pin 40 as an option.

Let me know how I may be able to do this, or if you have suggestions for an easier/more user friendly alternative.


Solution

  • Perhaps you could use list.pop(i) to remove items from a list once they are selected.
    https://docs.python.org/3/tutorial/datastructures.html

    #example:
    lst=[1,2,3,4,5,6,7,8,9,10]
    x = lst.pop(0)
    print(x)
    print(lst)