首页 > 解决方案 > 如何使用 tkinter 更新按键上的变量?

问题描述

我正在尝试修改一些代码来为我工作。我有一个正在运行的 tkinter 应用程序,当我按下按钮时我可以更新两个分数(蓝色和红色),但我想找到一种方法如何通过 keypress 做到这一点?例如,按“r”时增加红色的分数,按“b”时增加蓝色的分数尝试了谷歌的不同方法,但没有任何运气。有人可以看看并给我一些提示吗?

import tkinter as tk

from time import sleep

window = tk.Tk()

window.configure(bg='black')
window.geometry('1024x600')
window.overrideredirect(True)

WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)

scoreRed = 0
scoreBlue = 0

global BlueWonBoolean
global RedWonBoolean
BlueWonBoolean = False
RedWonBoolean = False

RedText = tk.StringVar()
BlueText = tk.StringVar()

RedText.set(str(scoreRed))
BlueText.set(str(scoreBlue))



def addBlue():
    global scoreBlue
    scoreBlue += 1
    BlueText.set(str(scoreBlue))
    if scoreBlue == 21:
        global BlueWonBoolean
        BlueWonBoolean = True
        print("\nBlue Won!!!\nBLUE | RED\n " + str(scoreBlue) + "  :  " + str(scoreRed))

        global BlueWon
        BlueWon = tk.Label(text="Blue Won!!!",
                           foreground="white",
                           background="black",
                           width=10,
                           height=10)
        BlueWon.pack(side=tk.TOP, fill=tk.X)


def addRed():
    global scoreRed
    scoreRed += 1
    RedText.set(str(scoreRed))
    if scoreRed == 21:
        global RedWonBoolean
        RedWonBoolean = True
        print("\nRed Won!!!\nRED | BLUE\n" + str(scoreRed) + "  :  " + str(scoreBlue))

        global RedWon
        RedWon = tk.Label(text="Red Won!!!",
                          foreground="white",
                          background="black",
                          width=10,
                          height=10)
        RedWon.pack(side=tk.TOP, fill=tk.X)


def resetScore():
    global scoreRed
    global scoreBlue
    global BlueWonBoolean
    global RedWonBoolean
    scoreRed = 0
    scoreBlue = 0
    RedText.set(str(scoreRed))
    BlueText.set(str(scoreBlue))
    BlueLabel.pack(side=tk.LEFT, fill=tk.X)
    RedLabel.pack(side=tk.RIGHT, fill=tk.X)

    if BlueWonBoolean == True:
        BlueWon.destroy()
        BlueWonBoolean = False
    elif RedWonBoolean == True:
        RedWon.destroy()
        RedWonBoolean = False

    BlueButton = tk.Button(window, text="Blue Point", bg="white", fg="yellow", width=30, height=15, command=addBlue)
    RedButton = tk.Button(window, text="Red Point", bg="red", fg="black", width=30, height=15, command=addRed)
    ResetButton = tk.Button(window, text="Reset", width=10, height=3, command=resetScore)

    BlueLabel.pack(side=tk.LEFT, fill=tk.X)
    RedLabel.pack(side=tk.RIGHT, fill=tk.X)


def Quit():
    exit()


while True:
    try:

        BlueLabel = tk.Label(
            textvariable=BlueText,
            foreground="white",
            background="black",
            width=10,
            height=5
        )

        RedLabel = tk.Label(
            textvariable=RedText,
            foreground="white",
            background="black",
            width=10,
            height=5
        )

        BlueButton = tk.Button(window, text="Blue Point", bg="black", fg="WHITE", width=30, height=15, command=addBlue)
        RedButton = tk.Button(window, text="Red Point", bg="black", fg="WHITE", width=30, height=15, command=addRed)
        ResetButton = tk.Button(window, text="Reset", bg="black", fg="WHITE", width=10, height=3, command=resetScore)
        quitButton = tk.Button(window, text="Quit ", bg="black", fg="WHITE", command=Quit)

        # image = tk.PhotoImage(file="cornHole.png")
        # imageLabel = tk.Label(image=image)

        BlueLabel.pack(side=tk.LEFT, fill=tk.X)
        RedLabel.pack(side=tk.RIGHT, fill=tk.X)

        BlueButton.pack(side=tk.LEFT, fill=tk.X)
        RedButton.pack(side=tk.RIGHT, fill=tk.X)
        # imageLabel.pack(side=tk.TOP, fill=tk.X)
        quitButton.pack(side=tk.TOP, fill=tk.X)
        ResetButton.pack(side=tk.TOP, fill=tk.X)
        window.bind("<Escape>", exit)
        window.mainloop()
    except:
        exit()

标签: pythontkinter

解决方案


您可以使用我们.bind()将任何键绑定到任何函数,唯一的要求是该函数将具有一些参数,例如“事件”。

def function(event):
    #Do something
window.bind("<Return>", function)

这样,每次按下按钮(在本例中为 Enter)时,都会调用该函数。在您的情况下,您可以将 2 个按钮“r”和“b”绑定到 2 个函数,以便为每一侧添加分数。

这是一些示例的链接: https ://www.geeksforgeeks.org/python-binding-function-in-tkinter/


推荐阅读