【问题标题】:How to update a label in python?如何在python中更新标签?
【发布时间】:2017-01-13 20:14:44
【问题描述】:

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

【问题讨论】:

  • “没用”是什么意思?你收到错误了吗?如果是这样,它很可能确切地说明了它为什么不起作用。此外,问题中代码的缩进在某些地方看起来不正确。
  • 对于我在编程和识别问题方面的糟糕技能,我深表歉意,但没有错误消息,尽管它没有按照我的意愿执行。我制作了一段关于它的作用的视频:gyazo.com/378a81554232f9421efb62b81f62f272 但我只希望它在一个标签上更新,而不创建许多其他标签。
  • 抱歉,您能否稍微编辑一下问题的代码部分,现在看来 uiPrint 函数没有做任何事情。您还可以在定义之前使用 click。
  • label['text'] = "Hello World"label["image"] = new_image
  • 顺便说一句:或label.config(text="Hello World")。其他小部件也是如此 - 即。 mainClickButton["command"] = stop_clickingmainClickButton.config(command=stop_clicking)

标签: python tkinter label photo tk


【解决方案1】:

我找到了这篇文章:

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 3.5,并且我不打算使用它的任何其他版本。
  • 用python 3测试过,效果很好,分数更新了
  • 完全按照我写的方式试一试。真正不同的是
  • click=StringVar(),因为当调用此对象上的 set() 命令时,它就像一个事件,因此包含它的标签会更新。
猜你喜欢
  • 1970-01-01
  • 2018-10-06
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多