所以我试着每次你点击一个没有炸弹的按钮,让变量“score”更新并立即显示在底部。但是当我单击其中一个时,由于某种原因显示的变量没有更新,即使我明确地说显示的“Label”具有“text=score”。我知道我的代码很长,效率不高,但我对python有点陌生,我还在学习。我可以在代码中修复什么来解决这个问题?感谢您的帮助!你知道吗
from tkinter import *
import random
screen = Tk()
ticket = random.randint(1,3)
score = 0
def test():
ticket1 = random.randint(1,3)
ticket2 = random.randint(1,3)
def test1():
if ticket1 == button1:
button_1 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_1.grid(row=1, column=0, sticky="w")
else:
button_2 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_2.grid(row=1, column=0, sticky="w")
global score
score += 1
def test2():
if ticket1 == button2:
button_3 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_3.grid(row=1, column=1, sticky="w")
else:
button_4 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_4.grid(row=1, column=1, sticky="w")
global score
score += 1
def test3():
if ticket1 == button3:
button_5 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_5.grid(row=1, column=2, sticky="w")
else:
button_6 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2)
button_6.grid(row=1, column=2, sticky="w")
global score
score += 1
def test4():
if ticket2 == button1:
button_1 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_1.grid(row=0, column=0, sticky="w")
else:
button_2 = Button(screen, text="+2", fg="white", bg="green", width=15, height=2)
button_2.grid(row=0, column=0, sticky="w")
global score
score += 2
def test5():
if ticket2 == button2:
button_3 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_3.grid(row=0, column=1, sticky="w")
else:
button_4 = Button(screen, text="+2", fg="white", bg="green", width=15, height=2)
button_4.grid(row=0, column=1, sticky="w")
global score
score += 2
def test6():
if ticket2 == button3:
button_5 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2)
button_5.grid(row=0, column=2, sticky="w")
else:
button_6 = Button(screen, text="+2", fg="white", bg="green", width=15, height=2)
button_6.grid(row=0, column=2, sticky="w")
global score
score += 2
button1 = Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test1)
button1.grid(row=1, column=0, sticky="w")
button1 = 1
button2 = Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test2)
button2.grid(row=1, column=1, sticky="w"+"e"+"n"+"s")
button2 = 2
button3 = Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test3)
button3.grid(row=1, column=2, sticky="e")
button3 = 3
button4 = Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test4)
button4.grid(row=0, column=0, sticky="w")
button4 = 1
button5 = Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test5)
button5.grid(row=0, column=1, sticky="w"+"e"+"n"+"s")
button5 = 2
button6 = Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test6)
button6.grid(row=0, column=2, sticky="e")
button6 = 3
button1 = Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test)
button1.grid(row=8, columnspan=3, sticky="w"+"e"+"n"+"s")
scoreText = Label(screen, text="Score: " + str(score), width=25, height=2)
scoreText.grid(row=9, columnspan=3, sticky="w"+"e"+"n"+"s")
screen.mainloop()
更改
score
后,必须替换标签中的文本替换按钮中的文本而不是创建新按钮的完整代码。你知道吗
相关问题 更多 >
编程相关推荐