2

I'm trying to make my program change the text based on a variable selected in the dropdown menu, but the button to activate the command doesn't seem to be working. From what I can see, the select function run's once the program is loaded and then never again, regardless of when I click the button.

from Tkinter import *

class App:

    def __init__(self, root):
        self.title = Label(root, text="Choose a food: ",
                           justify = LEFT, padx = 20).pack()
        self.label = Label(root, text = "Please select a food.")
        self.label.pack()

        self.var = StringVar()
        self.var.set("Apple")
        food = ["Apple", "Banana", "Pear"]
        option = apply(OptionMenu, (root, self.var) + tuple(food))
        option.pack()

        button = Button(root, text = "Choose", command=self.select())
        button.pack()

    def select(self):
        selection = "You selected the food: " + self.var.get()
        print(self.var.get()) #debug message
        self.label.config(text = selection)

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()

I'm a beginner on Tkinter, and I'm trying to figure out the basics before I go into making my full app. Thanks in advance :)

Rikg09
  • 165
  • 2
  • 2
  • 14

3 Answers3

9

Try changing button = Button(root, text = "Choose", command=self.select()) to button = Button(root, text = "Choose", command=self.select). Note the removed parentheses after self.select. This way, the method only gets referenced and not actually executed until you press the button.

Noshii
  • 486
  • 3
  • 12
2

Your main issue is you don't need the parentheses when setting command=self.food():

button = Button(root, text="Choose", command=self.select)

As a side-note, the way you generate your OptionMenu is slightly unusual. You can use the following instead, which is more consistent with the rest of your code:

option = OptionMenu(root, self.var, *food)
asongtoruin
  • 9,794
  • 3
  • 36
  • 47
  • Thanks, your answer solved my problem too, but Noshii beat you by a mere minute sorry! And thanks for the change to the options menu. – Rikg09 Aug 16 '17 at 09:59
0

enter image description hereThe command parameter documentation as per tkinterbook.

(A function or method that is called when the button is pressed. The callback can be a function, bound method, or any other callable Python object. If this option is not used, nothing will happen when the user presses the button.)

*****************************modified code*******************************

from Tkinter import *

class App:

    def __init__(self, root):
        self.title = Label(root, text="Choose a food: ",
                           justify = LEFT, padx = 20).pack()
        self.label = Label(root, text = "Please select a food.")
        self.label.pack()

        self.var = StringVar()
        self.var.set("Apple")
        food = ["Apple", "Banana", "Pear"]
        option = apply(OptionMenu, (root, self.var) + tuple(food))
        option.pack()

        button = Button(root, text = "Choose", command=self.select)
        #use function name instead of aclling the function
        button.pack()

    def select(self):
        selection = "You selected the food: " + self.var.get()
        print(selection) #debug message
        self.label.config(text = selection)

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()
mayure098
  • 111
  • 1
  • 5