python - Change OptionMenu based on what is selected in another OptionMenu -


I am currently trying to create two OptionMenu s, where the other is dynamically For example, I would like to create a OptionMenu_A with list

   
  [North America, Europe, Asia]   
  • If Asia is selected, then OptionMenu_B something like [Japan, China, Malasia] .
  • If Europe is selected, then it will convert to [Germany, France, Switzerland] for example.

    I am able to create two OptionMenu s but can not get the OptionMenu_B to update based on OptionMenu_A status

    Can anyone be very kind to show that such a thing is possible?

    Yes, this is possible. With StringVar.trace , you can check when the first option has been changed. Then remove all options in the second option menu and fill it with this option. If you have a data structure like a dictionary behind this, then this pattern can be very easy to map:

      import sys if sys.version_info [0]> = 3: import as tkkner Import TK class app (tk.Frame) as someone else Tkinter: def __init __ (self, master): tk.frame .__ init __ (self, master) self.dict = {' Asia ': [' Japan ',' China ',' Malaysia '],' Europe ': [' Germany ',' France ',' Switzerland ']} self.variable_a = tk.StringVar (self) self.variable_b = tk .StringVar (self) self.variable_a.trace ('w', self.update_options) self.opt Ionmenu_a = tk.OptionMenu (self, self.variable_a, * self.dict.keys ()) self.optionmenu_b = tk.OptionMenu (self, self.variable_b, '') self.variable_a .Set ('Asia') self. Optionmenu_a.pack () self.optionmenu_b.pack () self.pack () def update_options (self, * args): countries = self.dict [self.variable_a.get ()] self .variable_b.set (countries [0] ) Menu = self.optionmenu_b ['menu'] menu.delete (0, 'end') for countries in the country: menu.add_command (label = country, command = lambda nation = country: self.variable_b.set (nation) ) If __name__ == "__main__": root = tk.Tk () application = application (root) app.mainloop ()    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -