在python tkin中调用事件时不更新带有变量的标签

2024-04-25 19:49:54 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试着每次你点击一个没有炸弹的按钮,让变量“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()

Tags: textdefcolumnbuttonwidthscreengridrow
1条回答
网友
1楼 · 发布于 2024-04-25 19:49:54

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

相关问题 更多 >