Hello Python Users:
The following is a simple Python program. If you press the button with label red it prints "red". If you press the button with label blue it prints "blue".
One thing that frustrates me is that I have to write a function for every button. If I have 20 buttons I need 20 funcitons.
What I would like to do is have the same function for every button. Then using an "IF" statement it would print red if the red button was pressed and blue if the blue button was pressed. This would greatly simplify my code. Any suggestions?
The following is a simple Python program. If you press the button with label red it prints "red". If you press the button with label blue it prints "blue".
One thing that frustrates me is that I have to write a function for every button. If I have 20 buttons I need 20 funcitons.
What I would like to do is have the same function for every button. Then using an "IF" statement it would print red if the red button was pressed and blue if the blue button was pressed. This would greatly simplify my code. Any suggestions?
def red(): print("red") def blue(): print("blue") red = Button(root, text="Red button", bg="white", fg="red",relief = "ridge", font = "Helvitica 20", command=red) red.pack() blue = Button(root, text="blue button", bg="white", fg="blue", relief = "groove",font = "Helvitica 20",command = blue) blue.pack() root.mainloop()