Matthew Swart
Matthew Swart

Reputation: 41

Tkinter button command argument

I have been looking into tkinter with python as I am seriously interested in GUi's and thought it would be a great place to start. I went through a good few tutorials like The New Boston set and one or two theres to grab hold of the basics. Now I am trying to pass an 'argument' through a botton so that my program will move on to my IF statement and I am having no joy.

Please find attached code:

  try:
    from tkinter import *
except ImportError:
    from Tkinter import *
eod = 'no'
selection = []
selection1 = 'nothing'
while eod != 'yes':

    def beer():
        selection.append('Beer')
        selection1 = 'Beer'
    def wine():
        selection.append('Wine')
    def whiskey():
        selection.append('Whiskey')

    welcomeGUI = Tk()
    welcomeGUI.geometry('400x200+100+200')
    welcomeGUI.title('Drinks Despenser')

    welcomLabel1 = Label(welcomeGUI, text='Drinks-O-Matic', font='Times 22 bold').grid(row=0,column=2)
    welcomLabel2 = Label(welcomeGUI, text='Please select drink', font='Times 16 bold').grid(row=1,column=2)


    beerButton = Button(welcomeGUI, text='Beer', font='Times 16 bold',command=beer()).grid(row=6,column=1)
    wineButton = Button(welcomeGUI, text='Wine', font='Times 16 bold').grid(row=6,column=2)
    whiskeyButton = Button(welcomeGUI, text='Whiskey', font='Times 16 bold').grid(row=6,column=3)

    if selection1 is 'Beer':
        welcomeGUI.destroy()
        beerGUI = Tk()
        beerGUI.geometry('400x200+100+200')
        beerGUI.title('Beer Despenser')

        beerGUI.mainloop()


    welcomeGUI.mainloop()

Upvotes: 1

Views: 4210

Answers (1)

user5562628
user5562628

Reputation:

Ok there is a lot going on here so I have a couple of things that I think will help you.

You need to move your def out of the while loop for all the functions. They should be defined only once in the beginning of the file.

Also, you are assigning variables to the Button object after you call the grid method. That method returns None so you shouldn't do that because you are assigning variables None instead of the actual button object as you intend to. You should assign the variables the button object alone and then call varname.grid() later.

Finally, to address your question: when you write command=beer() you are once again calling the function beer and assigning its return value to the command parameter. When you are using Tkinter you must assign only the function name to the command parameter such as command=beer. However, if you have to pass it arguments you can use lambda. For example: command=lambda: beer(arg1, arg2).

P.S. When comparing strings you should say

if selection1 == "Beer":

not

if selection1 is "Beer":

is tests for identity not equality and you want to test equality.

EDIT: You also should unindent the try at the top of your file.

Also because selection1 is a local variable in the function beer it won't work, you need to declare it a global

def beer():
    global selection1
    selection.append('Beer')
    selection1 = 'Beer'

Furthermore, you need to destroy the window or the if statement in the while loop won't run.

def beer(window):
    global selection1
    selection.append('Beer')
    selection1 = 'Beer'
    window.destroy()

and then you need to pass the welcomeGUI Tk instance to the function like so

beerButton = Button(welcomeGUI, text='Beer', font='Times 16 bold',command=lambda: beer(welcomeGUI)).grid(row=6,column=1)

One last thing. I would remove the while loop all together and have a button on the beer window to call back the main welcome window because putting two mainloops in the while loop is not going to be a good thing.

Upvotes: 2

Related Questions