如何更新 python 中的标签?
How to update a label in python?
我是 python 的新手,当我遇到问题时正在创建一个小游戏。我搜索了一个答案,找到了一个,但它没有用。
我正在尝试制作的游戏或多或少是 Cookie Clicker 的娱乐,我正在尝试制作我有更新分数的标签,相反,它创建了一个新标签。
from tkinter import *
import time
master = Tk()
def uiPrint():
print("")
print(click)
blankLine()
click = 0
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global click
global mult
click += 1*(mult)
uiPrint()
scoreCommand = Button(text=click)
scoreCommand.pack()
mainClickButton = Button(master, text="Click me!", command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
label = Label(image=photo)
label.image = photo
label.pack()
mainloop()
我找到这个 post :
Update Tkinter Label from variable
稍微修改一下,意思是:
from tkinter import *
master = Tk()
def uiPrint():
print("")
print(clickcount)
blankLine()
clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global clickcount
global click
global mult
clickcount += 1*(mult)
click.set(str(clickcount)); #Update score
uiPrint()
mainClickButton = Button(master, text="Click me!", command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
label = Label(image=photo)
label.image = photo
label.pack()
l = Label(master, textvariable = click) #Score
l.pack()
mainloop();
我还建议将 cookie 图像制作为按钮:
from tkinter import *
master = Tk()
def uiPrint():
print("")
print(clickcount)
blankLine()
clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global clickcount
global click
global mult
clickcount += 1*(mult)
click.set(str(clickcount)); #Update score
uiPrint()
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
mainClickButton = Button(master, image=photo, command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
l = Label(master, textvariable = click) #Score
l.pack()
mainloop();
两者均使用 python 2.7.11 和 python 3.5.2 进行了测试,它们工作正常
我是 python 的新手,当我遇到问题时正在创建一个小游戏。我搜索了一个答案,找到了一个,但它没有用。
我正在尝试制作的游戏或多或少是 Cookie Clicker 的娱乐,我正在尝试制作我有更新分数的标签,相反,它创建了一个新标签。
from tkinter import *
import time
master = Tk()
def uiPrint():
print("")
print(click)
blankLine()
click = 0
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global click
global mult
click += 1*(mult)
uiPrint()
scoreCommand = Button(text=click)
scoreCommand.pack()
mainClickButton = Button(master, text="Click me!", command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
label = Label(image=photo)
label.image = photo
label.pack()
mainloop()
我找到这个 post :
Update Tkinter Label from variable
稍微修改一下,意思是:
from tkinter import *
master = Tk()
def uiPrint():
print("")
print(clickcount)
blankLine()
clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global clickcount
global click
global mult
clickcount += 1*(mult)
click.set(str(clickcount)); #Update score
uiPrint()
mainClickButton = Button(master, text="Click me!", command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
label = Label(image=photo)
label.image = photo
label.pack()
l = Label(master, textvariable = click) #Score
l.pack()
mainloop();
我还建议将 cookie 图像制作为按钮:
from tkinter import *
master = Tk()
def uiPrint():
print("")
print(clickcount)
blankLine()
clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0
def blankLine():
for i in range(20):
print("")
def buttonCommand():
global clickcount
global click
global mult
clickcount += 1*(mult)
click.set(str(clickcount)); #Update score
uiPrint()
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
mainClickButton = Button(master, image=photo, command = buttonCommand)
mainClickButton.pack()
master.title("Cookie Clicker")
l = Label(master, textvariable = click) #Score
l.pack()
mainloop();
两者均使用 python 2.7.11 和 python 3.5.2 进行了测试,它们工作正常