我正在用TK编写一个简单的计分应用程序,但我遇到了一些问题。 首先,我无法根据IF条件在网格内放置标签,其次才能将标签更改为其他文本。例如,“游戏正在运行”,结束时将文本更改为“游戏结束”,然后将其删除。为了处理这种情况,我使用了一个简单的整数变量。 当我运行代码时,我没有得到任何错误,可以从中获得一些信息。
所以我有两个目标:
根据if条件在同一位置显示不同的文本
要根据elif条件再次删除此文本。
我将不胜感激
from tkinter import *
# environment set up
root = Tk()
root.overrideredirect(False)
root.title("Scoreboard")
root.geometry("1024x600")
root.configure(background="white")
root.focus_set()
for b in range(12):
root.grid_columnconfigure(b, minsize=85)
for i in range(6):
root.grid_rowconfigure(i, minsize=100)
# variables
counterLeft = 0
counterRight = 0
countTotal = 0
# variables convert to StringVar
counterLeftVar = StringVar()
counterLeftVar.set(str(counterLeft))
counterRightVar = StringVar()
counterRightVar.set(str(counterRight))
counterTotalVar = StringVar()
counterTotalVar.set(str(countTotal))
# function to monitor key action
def keyaction(e):
global counterLeft
global counterRight
global countTotal
global BlueWon
if e.char == "i":
counterLeft += 1
countTotal += 1
counterLeftVar.set(str(counterLeft)), e.char
counterTotalVar.set(str(countTotal)), e.char
elif e.char == "o":
counterRight += 1
countTotal += 1
counterRightVar.set(str(counterRight)), e.char
counterTotalVar.set(str(countTotal)), e.char
elif e.char == "r":
counterLeft = counterRight = 0
counterLeftVar.set(str(counterLeft)), e.char
counterRightVar.set(str(counterRight)), e.char
# total score
totCount = Label(root, font=("Arial", 48), textvariable=counterTotalVar, bg="white").grid(column=5, row=1)
# Score on the screen
left = Label(root, font=("Arial", 48), textvariable=counterLeftVar, bg="white").grid(column=4, row=3)
middle = Label(root, text=" : ", font=("Arial bold", 40), bg="white").grid(column=5, row=3)
right = Label(root, font=("Arial", 48), textvariable=counterRightVar, bg="white").grid(column=6, row=3)
if countTotal > 10:
BlueWon = Label(root, font=("Arial", 20), text="Game Over", bg="white").grid(column=0, row=0)
elif countTotal > 0:
BlueWon = Label(root, font=("Arial", 20), text="Game running", bg="white").grid(column=0, row=0)
def close(e):
root.destroy(), e.char
root.bind("<Key>", keyaction)
# root.bind("i", keyaction)
# root.bind("t", keyaction)
root.bind("q", close)
root.bind("<Escape>", exit)
root.mainloop()