Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.get issue
#1
Hey guys I am ashamed, but I stole dependent Choicebox code below from somewhere in StackOverFlow (I think) it's working as it should, when I choose from the Drop Down, the second one changes.
But I can't .get() the second choice by code it changes to the first choice.

So for example when we choose in the first choicebox the number 424, the second choice box creates dropdown according to the self.dict and will set the self.variable_b to the first choice of the list in this example "some".

When I select "some2" I it doesn't change self.variable_b() to the new selection (some2) and prints "some".
Any ideas on that?

class App(tk.Frame):
    def __init__(self, tab1):
        tk.Frame.__init__(self, tab1)

        self.dict = {'-':'-','424': ['some', 'some2', 'Malaysia'],
                     '3213': ['Germany', 'France', 'Switzerland'],
                     '1231231':['test','test1'],
                     '54654':['te2','te3'],
                     "7898":['doc1','doc2'],
                     "512":['1','2'],
                     "325":['tst','tst1'],
                     "4+63":['something','else'],
                     "7532":['Πολλοί 1','πολλοί 2']}

        self.variable_a = tk.StringVar(self)
        self.variable_b = tk.StringVar(self)
        var_a=self.variable_a
        var_b=self.variable_b

        self.variable_a.trace('w', self.update_options)

        self.optionmenu_a = tk.OptionMenu(tab1, self.variable_a, *self.dict.keys())
        self.optionmenu_b = tk.OptionMenu(tab1, self.variable_b, '')
        option_a=self.optionmenu_a
        option_b=self.optionmenu_b

        self.variable_a.set('-')
        canvas1.create_window(400,60,window=option_a)
        canvas1.create_window(400,100,window=option_b)
        self.pack()



    def update_options(self, *args):
        hospitals = self.dict[self.variable_a.get()]
        self.variable_b.set(hospitals[0])

        menu = self.optionmenu_b['menu']
        menu.delete(0, 'end')

        for hospital in hospitals:
            menu.add_command(label=hospital, command=lambda doctor=hospital: self.variable_b.set(doctor))

        global Doc
        global Hosp
        Hosp = self.variable_a.get()
        Doc = self.variable_b.get()
        print(Hosp)
        print(Doc)


app = App(tab1)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020