Wednesday 15 January 2014

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


I am currently trying to create two OptionMenu s, where the dynamically For example, for example, I would like to create a OptionMenu_A 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 Other: Import the tanker as vaccine class app (vaccine frame): def __init __ (self, master): TC. Frame .__ init __ (self, master) self.dict = {'Asia': ['Japan', 'China' '' Malaysia '],' Europe ': [' Germany ',' France ',' Switzerland '] } Self. WAIRABLE_A = vaccines. Stringwara (self) self Variable_b = tk.StringVar (self) self.variable_a.trace ('W', self.update_options) self.optionmenu_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 (auto, * args): Country = self.dict [self.variable_a.get ()] .variable_b.set (country [0]) menu = self.optionmenu_b ['menu'] menu.delete (0, 'end') for country in own countries : Menu.add_command (label = country, command = lambda nation = country: auto. Variable_b.set (country)) if __name__ == "__main__": root = tk.Tk () app = app (root) app.mainloop ( )    

No comments:

Post a Comment