Home > Blockchain >  If statement with buttons
If statement with buttons

Time:12-29

so I have a problem. Im trying to make a program with buttons, so you could choose something with buttons. Im running python 3.

I made the buttons, customized them, everything is good with the buttons.

def choiceFirst():
    choice = 1
    buttonPressed = True

def choiceSeco():
    choice = 2
    buttonPressed = True

window = Tk()
window.title("Title lol")
icon = PhotoImage(file='download.png')
window.geometry("300x215")
window.iconphoto(True,icon)

button1 = Button(window,
                text="Choice 1",
                command=choiceFirst,
                font=("Arial", 20),
                fg='#00f000',
                bg='white',
                relief=RAISED,
                bd=10,
                padx=20,
                pady=20,
                activeforeground='#00f000',
                activebackground="white",)
button2 = Button(window,
                text="Choice 2",
                command=choiceSeco,
                font=("Arial", 20),
                fg='#00f000',
                bg='white',
                relief=RAISED,
                bd=10,
                padx=15,
                pady=15,
                activeforeground='#00f000',
                activebackground="white",)

button1.pack()
button2.pack()
window.mainloop()

Now I want to add an if statement. Something like

if buttonPressed:
   if choice == 1:
      print("choice 1 code here")
   elif choice ==2:
      print("Choice 2 code here")

I tried adding that, it just doesn't work :(

All help is apreciated!

CodePudding user response:

(Sorry for bad english) You have to put the actions of the buttons in their functions.

Try this:

def choiceFirst():
    print("choice 1 code here")

def choiceSeco():
    print("Choice 2 code here")

and just remove the if statement.

  • Related