python - 在 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,并且我正在尝试在用户购买了增量器后更新价格标签。单击按钮后如何更新标签?
解决方案
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
编辑的标签显示标签更新而不是标签。
推荐阅读
- c# - 如何从 WebForms 重定向到 MVC 视图?
- mysql - MYSQL,如果值对不存在则创建行
- php - Wordpress如何禁用溢出
- php - 用户的一个信息和另一侧图片
- docker - 无法使用 Redis 连接到单独的 docker 容器
- java - How to turn off OSR compilation
- h5py - 我需要手动关闭 HDF5 文件吗?
- mongodb - Find({ example: { $elemMatch: { $eq: userId } } }).. in Aggregate - 有可能吗?
- swift - 无法使用 Xcode 10.2.1 在子 NSManagedObject 中存储自定义数据
- c - 从 UTF8 转换为 UTF16 LE 的概念 c 编程中的数学运算