如何在python中更新标签?

2024-10-03 02:48:07 发布

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

我是python新手,当我遇到问题时,我正在创建一个小游戏。我找了一个答案,找到了一个,但没用。在

我尝试做的游戏或多或少是一个曲奇点击器的再创造,我试图制作一个新的标签,我有更新的分数,相反,它创建了一个新的标签。在

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

Tags: importmasterdef标签globallabelpackclick
1条回答
网友
1楼 · 发布于 2024-10-03 02:48:07

我发现了这个帖子:

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图像设为按钮:

^{pr2}$

这两个版本都用python2.7.11和python3.5.2进行了测试,结果都很好

相关问题 更多 >