tkinter - Python 3.4 GUI if statement with buttons -


i'm trying learn coding python 3.4. built mini gui , i'm having trouble getting work. prints out 2 every time though press button 1 . want 1 printed out in label when click button on , 2 button 2 if statement.

from tkinter import * root = tk() root.geometry("500x500") root.title("test")   def button_press():     if one:         x = 1         display.configure(text=x)     if two:         x = 2         display.configure(text=x)  display = labelframe(root, bg="red", width="462", height="60") display.pack()  1 = button(root, text="1", width="15", height="5",command=button_press) one.pack(side="left")   2 = button(root, text="2", width="15", height="5", command=button_press) two.pack(side="left")  root.mainloop() 

you have 2 ways:

or using different function each button

or pass lambda 1 parameter.

bt … command = fct1 bt … command = fct2 

or use lambda…

from tkinter import * root = tk() root.geometry("500x500") root.title("test")   def button_press(var):     display.configure(text=var)  display = labelframe(root, bg="red", width="462", height="60") display.pack()  1 = button(root, text="1", width="15", height="5",command=lambda : button_press(1)) one.pack(side="left")   2 = button(root, text="2", width="15", height="5", command=lambda : button_press(2)) two.pack(side="left")  root.mainloop() 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -