Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If statement with buttons

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

(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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading