首页 > 解决方案 > 在 Tkinter 中单击按钮后如何更新标签?

问题描述

from tkinter import *

win = Tk()
win.minsize(250, 250)
win.title("Point Clicker")
win.resizable(0, 0)

# Make Save fuction
SaveButton = Button(win, text="Save Progress", command=None)
SaveButton.place(x=75, y=215)

value = 1
IncreaserPrice = 50
point = 0
print("You have " + str(point) + " points")

def AddPoint():
    global point
    point = point + value
    print("You have " + str(point) + " points")

def BuyDoubler():

    global value, point, IncreaserPrice

    if point > IncreaserPrice or point == IncreaserPrice:
        print("You have bought the Point Doubler")
        value = value + 1
        point = point - IncreaserPrice
        IncreaserPrice += 50
        Price1 = Label(win, text="Price = " + str(IncreaserPrice) + " points")
        print("You have " + str(point) + " points left")


    else:
        print("You dont have enough points for this")


Clicker = Button(win, text="Click for points", command=AddPoint)
Clicker.pack()

ClickIncreaser = Button(win, text="Buy Point Doubler", command=BuyDoubler)
ClickIncreaser.pack()

Price1 = Label(win, text="Price = " + str(DoublerPrice) + " points")
Price1.pack()

win.mainloop()

我正在尝试使用 python Tkinter 制作 Knock-Off Cookie Clicker,并且我正在尝试在用户购买了增量器后更新价格标签。单击按钮后如何更新标签?

标签: pythontkinter

解决方案


pack您可以在已经ing 或grid使用方法 ing之后更新 tkinter 标签Label.config。这是一个例子:

import tkinter as tk

screen = tk.Tk()
label = tk.Label(text="Label")
label.pack()
label.config(text="Label updated")
screen.mainloop()

pack编辑的标签显示标签更新而不是标签。


推荐阅读