I'm trying to learn Tkinter, and learning how to set a variable that can be worked on later on in the code from the choice selected from an optionmenu. Only been coding tkinter or python for about 2 days properly now and unsure why this doesn't work.
my code:
root = Tk()
root.title("test")
p1aspVARIABLE = str()
def tellmethevariable():
if p1aspVARIABLE == "AA":
print(p1aspVARIABLE)
else:
print("Not 'AA'")
def tellmeP1Asp(choice):
choice = p1aspCHOICE.get()
print(choice)
p1aspVARIABLE = choice
print(p1aspVARIABLE)
alleles = ["AA", "Ab", "bb"]
p1aspCHOICE = StringVar()
p1aspCHOICE.set(alleles[0])
alleledropdown = OptionMenu(root, p1aspCHOICE, *alleles, command=tellmeP1Asp)
button1 = Button(root, command=tellmethevariable)
alleledropdown.grid(row=0, column=0)
button1.grid(row=0, column=1)
root.mainloop()
I don't understand why the code is able to print out the p1aspCHOICE when ran from the "tellmeP1Asp", but not when done through "tellmethevariable"? I'm not sure how to get the p1aspVARIABLE to properly change to what was chosen in the OptionMenu list?
I'm not knowledgeable enough of what I'm doing wrong to properly google this, have been trying for a few hours now but to no avail.
What I tried: I've set this up in countless ways over the last few hours, mostly last night before giving up. This is actually one of the only versions of this code that ran.
What I was expecting: When the button1 Button is clicked, for it to either print the choice (if it was "AA", so it would print "AA"), or, if it wasn't "AA", it would print "Not 'AA'"