Search code examples
pythontkinteroptionmenu

Assign OptionMenu Variable in Python


I am working on a menu system that allows user to select date and location to access a specific file. I know it will be a lot of hard coding for each specific file. I want to use an OptionMenu system. I am getting values printed, but how can i define these values and pass them through a function to open that specific file. I am thinking a long if else statement. (IE if Monday && a then pass this call function).

Here is my code

#mainmenu
class MyOptionMenu(OptionMenu):
 def __init__(self, master, status, *options):
    self.var = StringVar(master)
    self.var.set(status)
    OptionMenu.__init__(self, master, self.var, *options)
    self.config(font=('calibri',(20)),bg='white',width=20)
    self['menu'].config(font=('calibri',(10)),bg='white')

root = Tk()
#attemtping to assign numerical values
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
mymenu1 = MyOptionMenu(root, 'Select day', 'Monday','Tuesday','Wednesday', 'Thursday', 'Friday')
mymenu2 = MyOptionMenu(root, 'Select Location', 'd','e','f')
#menus come up fine and values correctly printed
def ok():
print "value is", (mymenu1.var).get(), (mymenu2.var).get()
button = Button(root, text="OK", command=ok)
button.pack()
mymenu1.pack()
mymenu2.pack()
(mymenu1.var).get()
(mymenu2.var).get()
#assign variable x to return values
x = (mymenu1.var).get()
if x <2:
    print 'Negative changed to zero'
elif x == 0:
    print 'Zero'
elif x == 1:
    print 'Single'
else:
    print 'More'

root.mainloop()

I am getting this as an output "More"/ "value is Monday e", which shows I am able to access the correct outcome, but I am lost on implementing that variable (Tuesday) in the next step.


Solution

  • @JustForFun, your question is a little tangled and a bit hard to contemplate, but I think i understand what you want. Firstly you have put the last part at the wrong place (from #assign variable x to return values), this will be run through at the start, but not after the ok button is clicked, so x will always equal 'Select day' (thus why you get more printed when you run it), you should put this inside a function to be called in ok(), or in ok() itself:

    def ok():
        print "value is", (mymenu1.var).get(), (mymenu2.var).get()
        x = (mymenu1.var).get()
        if x <2:
            print 'Negative changed to zero'
        elif x == 0:
            print 'Zero'
        elif x == 1:
            print 'Single'
        else:
            print 'More'
    

    This will get the value for x and test it when ok is clicked, and it can lead to more actions like using the results gained to open a file etc. BUT, you have (I think) just rushed the last part with the if/elif etc statements as the first if...: will pick up items below 2 (<), so the next two elif statements aren't going to evaluate as true, do you intend to have (>) in the first statement? Also you probably need to include a variable for the second optionmenu in the if/elif statements:

    if x > 2 and y == ...:
        # open specific file? etc...