Я пытаюсь сделать игру, используя Tkinter, считая до 21 (2 игрока по очереди выбирают число от 1 до 3, при этом человек вынужден выбрать 21 проигрыш). На данный момент мой код создает интерфейс, но я не уверен, как заставить кнопки добавлять 1,2 или 3 к общему числу при нажатии.
(вот код ниже, любая помощь будет с благодарностью)
from random import randrange
window = tk.Tk()
window.title("Guessing Game")
lblInst = tk.Label(window, text = "Pick a number between 1-3")
lblLine0 = tk.Label(window, text = "*********************************************************************")
lblLine1 = tk.Label(window, text = "*********************************************************************")
lblLogs = tk.Label(window, text="Game Logs")
totnumber = ''
#buttons
def press(playernu):
global totnumber
totnumber = totnumber + str(playernu)
totnumber.set(totnumber)
# create the buttons
button1 = tk.Button(window, text=' 1 ', fg='black', bg='red',
command=lambda: press(1), height=1, width=7)
button2 = tk.Button(window, text=' 2 ', fg='black', bg='red',
command=lambda: press(2), height=1, width=7)
button3 = tk.Button(window, text=' 3 ', fg='black', bg='red',
command=lambda: press(3), height=1, width=7)
# append elements to grid
lblInst.grid(row=0, column=0, columnspan=5)
lblLine0.grid(row=1, column=0, columnspan=5)
button1.grid(row=2, column=0, columnspan=3)
button2.grid(row=2, column=1, columnspan=3)
button3.grid(row=2, column=2, columnspan=3)
lblLine1.grid(row=3, column=0, columnspan=5)
lblLogs.grid(row=4, column=0, columnspan=5)
# Main game logic
def process():
global totnumber, playernu, button1, button2, button3
while totnumber < 21:
lbl = tk.Label(window, text="It is your turn ", fg="black")
totnumber = totnumber + playernum
lbl = tk.Label(window, text="It is your turn "+ str(totnumber), fg ="black")
if totnumber > 20:
lbl = tk.Label(window, text="You Lose "+ str(totnumber), fg="red")
break
else:
pass
#computer turn
PCnum = random.randrange(1, 3)
lbl = tk.Label(window, text="It is now my turn ", fg="black")
lbl = tk.Label(window, text="I pick "+ str(PCnum), fg="black")
totnumber = PCnum + totnumber
lbl = tk.Label(window, text="the total number is now " + str(totnumber), fg="black")
if totnumber > 20:
lbl = tk.Label(window, text="welp I guess I lose this one traveller ", fg="green")
break
else:
pass```