【问题标题】:Change label text tkinter更改标签文本 tkinter
【发布时间】:2017-02-18 21:38:14
【问题描述】:

我从 usingpython.com 获得此代码,这是一个“输入颜色而不是单词”game

我正在使用此代码来构建此游戏的改进版本,但出现了问题,我不知道为什么。

所以,当倒计时为 0 时,我想将单词所在的标签(名为“标签”)更改为“游戏结束!你的分数是 bla bla bla”。所以,我这样做了(我添加的只是最后两行):

def nextColour():

#use the globally declared 'score' and 'play' variables above.
global score
global timeleft

#if a game is currently in play...
if timeleft > 0:

    #...make the text entry box active.
    e.focus_set()

    #if the colour typed is equal to the colour of the text...
    if e.get().lower() == colours[1].lower():
        #...add one to the score.
        score += 1

    #clear the text entry box.
    e.delete(0, tkinter.END)
    #shuffle the list of colours.
    random.shuffle(colours)
    #change the colour to type, by changing the text _and_ the colour to a random colour value
    label.config(fg=str(colours[1]), text=str(colours[0]))
    #update the score.
    scoreLabel.config(text="Score: " + str(score))

elif timeleft == 0:
    ĺabel.config(text="Game Over! Your score is: " + score)

这不起作用。当倒计时为 0 时,游戏什么也不做并停止。

我在想我是否可以用一个while循环来做到这一点......

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    更新小部件值

    更多详情请见this answer

    您可以使用StringVar 对象的textvariable 选项或Label 对象的.configure() 方法“动态”更改标签小部件的文本值。正如上面的答案中提到的,.configure() 方法的好处是要跟踪的对象更少

    使用textvariableStringVar

    # Use tkinter for Python 3.x
    import Tkinter as tk
    from Tkinter import Label
    
    root = tk.Tk()
    
    # ...
    my_string_var = tk.StringVar(value="Default Value")
    
    my_label = Label(root, textvariable=my_string_var)
    my_label.pack()
    
    #Now to update the Label text, simply `.set()` the `StringVar`
    my_string_var.set("New text value")
    

    .configure()

    # ...
    
    my_label = Label(root, text="Default string")
    my_label.pack()
    
    #NB: .config() can also be used
    my_label.configure(text="New String")
    

    更多详情请见effbot.org

    调试检查

    在不查看您的所有代码的情况下,我还建议您检查下面列出的各种其他问题以了解可能的原因。 为了扩展您的 cmets(在这篇文章中),程序不能按预期“工作”可能有多种原因:

    • 程序永远不会进入最后的 if 块 (if timeleft == 0),因此 .config 方法没有机会更新变量
    • 全局变量timeleft 确实达到0,但在该迭代之后,它在0 以上递增并重新进入第一个if 块(if timeleft>0),覆盖您想要的.config()
    • 代码的另一部分可能是在您的小部件上调用 .config() 并覆盖您所需的更改

    规划您的 GUI

    为防止这些事情发生,我强烈建议您退后一步,准备一些笔和纸,并考虑一下您的应用程序的整体设计。具体问问自己:

    • 用户如何与这个小部件交互?哪些操作/事件会导致此小部件发生变化?
    • 想想这些事件的所有组合,并问问自己这些事件是否相互冲突。

    还可以考虑为应用绘制流程图,从用户启动应用到关闭之前可以采用的可能路径,确保流程中的块不会相互矛盾。

    最后,还请查看Model-View-Controller 架构(及其variants)以获得良好的应用程序设计

    【讨论】:

    • 注意:你没有使用textvariable。您可以通过调用标签上的configure 方法来更改文本。
    • 哦,从来不知道 - 一直这样做,没有考虑太多。认为我应该记下我的答案吗? (认为​​这是问题)
    • 你不必记下你的答案,但我建议稍微改写一下,以免给人留下这样的印象,这是唯一的方法。
    • Bryan Oakley,这就是我正在做的事情,但没有成功。我使用了 label.config....
    • 我几乎明白了...但是我怎么能把:fg=str(colours[1]), text=str(colours[0])放在一个值里面=
    【解决方案2】:

    初始标签-

        lbl_selection_value1=Label(root, text="Search Option 1")
        lbl_selection_value1.grid(row=0,column=0,padx=1)
    

    更新标签-

        lbl_selection_value1.destroy()
        lbl_selection_value1_updated = Label(root, text='New Text')
        lbl_selection_value1_updated.grid(row=0, column=0, padx=1)
    

    【讨论】:

    • 如果您所做的只是更改文本,那么绝对没有理由销毁和重新创建标签。另外,在最初的问题中,他们使用的是pack,所以这个答案对他们不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多