0

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!

  • 1
    in your choiceFirse/Seco functions you're trying to change `choice` - but at this point, those are local variables. Add `global choice, buttonPressed` as first line in those functions to make those variables global and change them globally – h4z3 Dec 29 '21 at 10:27
  • Also, you're in window's main loop. You could, you know, actually put that `"choice 1 code here"` in choiceFirst function. – h4z3 Dec 29 '21 at 10:30

1 Answers1

0

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

Davide Antipa
  • 92
  • 1
  • 6