Python 单击Tkinter中的按钮后如何更新标签?
我正在尝试使用python Tkinter制作一个仿制的Cookie Clicker,并且在用户购买增量器之后,我正在尝试更新价格标签。Python 单击Tkinter中的按钮后如何更新标签?,python,tkinter,Python,Tkinter,我正在尝试使用python Tkinter制作一个仿制的Cookie Clicker,并且在用户购买增量器之后,我正在尝试更新价格标签。 单击按钮后如何更新标签?所有这些参数都可作为对象的属性使用。您需要更改现有对象,而不是创建新对象 from tkinter import * win = Tk() win.minsize(250, 250) win.title("Point Clicker") win.resizable(0, 0) # Make Save fuctio
单击按钮后如何更新标签?所有这些参数都可作为对象的属性使用。您需要更改现有对象,而不是创建新对象
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()
你知道你可以在
if
语句中使用=
而不是两个单独的比较吗?你可以在打包
或网格
使用label.config
方法打包后更新tkinter标签。下面是一个例子:
Price1.text = "Price = %d points" % IncreaserPrice
已被
pack
ed更新的标签显示标签已更新,而不是标签。哦,是的,我不小心使用了两个运算符,而不是一个。@Debil.exe非常希望您能继续您有趣的小项目。谢谢!我将来也会做很多游戏!谢谢你的帮助!
import tkinter as tk
screen = tk.Tk()
label = tk.Label(text="Label")
label.pack()
label.config(text="Label updated")
screen.mainloop()