在python tkinter中调用事件时,变量不更新的标签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python tkinter中调用事件时,变量不更新的标签相关的知识,希望对你有一定的参考价值。

因此,我每次点击其中一个没有炸弹的按钮时,我会尝试更新变量“得分”并立即显示在底部。但是当我单击其中一个时,显示的变量由于某种原因没有更新,即使我明确地说“标签”显示为具有“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后,您必须替换标签中的文本

 score += 1
 scoreText['text'] = "Score: " + str(score)

完整代码替换按钮中的文本而不是创建新文本。

import tkinter as tk
import random

def test():

    def test1():
        global score

        if ticket1 == 1:
            button1.config(text="RIP", bg="red")
        else:
            button1.config(text="+1", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)

    def test2():
        global score

        if ticket1 == 2:
            button2.config(text="RIP", bg="red")
        else:
            button2.config(text="+1", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)

    def test3():
        global score

        if ticket1 == 3:
            button3.config(text="RIP", bg="red")
        else:
            button3.config(text="+1", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)

    def test4():
        global score

        if ticket2 == 1:
            button4.config(text="RIP", bg="red")
        else:
            button4.config(text="+2", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)

    def test5():
        global score

        if ticket2 == 2:
            button5.config(text="RIP", bg="red")
        else:
            button5.config(text="+2", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)

    def test6():
        global score

        if ticket2 == 3:
            button6.config(text="RIP", bg="red")
        else:
            button6.config(text="+2", bg="green")
            score += 1
            label_score['text'] = "Score: " + str(score)


    ticket1 = random.randint(1, 3)
    ticket2 = random.randint(1, 3)

    button1 = tk.Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test1)
    button1.grid(row=1, column=0, sticky="w")

    button2 = tk.Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test2)
    button2.grid(row=1, column=1, sticky="wens")

    button3 = tk.Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test3)
    button3.grid(row=1, column=2, sticky="e")

    button4 = tk.Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test4)
    button4.grid(row=0, column=0, sticky="w")

    button5 = tk.Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test5)
    button5.grid(row=0, column=1, sticky="wens")

    button6 = tk.Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test6)
    button6.grid(row=0, column=2, sticky="e")

# --- main --

score = 0

screen = tk.Tk()

button_start = tk.Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test)
button_start.grid(row=8, columnspan=3, sticky="wens")

label_score = tk.Label(screen, text="Score: 0", width=25, height=2)
label_score.grid(row=9, columnspan=3, sticky="wens")

screen.mainloop()

以上是关于在python tkinter中调用事件时,变量不更新的标签的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 3 和 tkinter 中使用变量调用函数

Python tkinter点击事件未处理

用python的Tkinter中的按钮,绑定的事件如何更改全局变量的问题

Python:GUI之tkinter学习笔记3事件绑定

Python & Tkinter -> 关于调用冻结程序的长时间运行函数

在python tkinter中为事件创建日历视图[关闭]