Home > database >  create a button that can only be pressed once
create a button that can only be pressed once

Time:08-24

i am trying to create a button that can only be pressed once for a game in python but i cant figure out how to set a variable that works in the def and does not change every time the function is run.

tut = turtle.Screen()._root
def bt1():
    pressed = 1
    x_turn = True
    if (pressed == 1):
        if (x_turn is True):
            print("this button has not been pressed as player1")
            x_turn == False
            pressed = 0
        else:
            print ("this button has not been pressed as player2")
            pressed = 0
            x_turn == True
    else:
        print("this button has been pressed")

photo1 = Image.open(r"C:\image.png")
resize_photo1 =photo1.resize((50,50), Image.ANTIALIAS)
print_photo1 = itk.PhotoImage(resize_photo1)
Button(tut, image= print_photo1,command=bt1).pack(side= LEFT)

CodePudding user response:

what i am gathering is that when this button is pressed you want it to set pressed to 0 so it can not run again but since you put the variable at the top every time the button is bressed it will set it to 1. to fix this all you want to do is....

tut = turtle.Screen()._root
global pressed
pressed = 1
global x_turn
x_turn = True
def bt1():
    if (pressed == 1):
        if (x_turn is True):
            print("this button has not been pressed as player1")
            x_turn == False
            pressed = 0

This might work let me know if it does

CodePudding user response:

x_turn = True

Try using this :

x_turn == True

I think here you’re missing the equals to sign and that’s why it is creating a problem.

  • Related