【问题标题】:Label with variable not updating when event is called in python tkinter在 python tkinter 中调用事件时,带有变量的标签不更新
【发布时间】:2017-12-19 17:30:49
【问题描述】:

因此,我尝试每次单击没有炸弹的按钮之一,以更新变量“分数”并立即显示在底部。但是,当我单击其中一个时,即使我明确表示要显示的“标签”具有“文本=分数”,由于某种原因,显示的变量也不会更新。我知道我的代码很长而且效率不高,但我对 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()

【问题讨论】:

  • 而不是text= 使用textvariable=tk.StringVar() 或直接更新scoreText['text'] = "new text"
  • 为了让代码更易读,把global放在函数开头,def前面加空格
  • 顺便说一句:最好更改按钮 button_1['text'] = "new text" 中的文本,而不是创建新按钮。现在您可能在一个单元格中有许多按钮 - 一个按钮在另一个之上。

标签: python variables tkinter label


【解决方案1】:

更改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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2015-11-01
    • 1970-01-01
    • 2011-02-05
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多