[英]Change label text tkinter
I got this code from usingpython.com which is a "type the colour not the word" game .我从 usingpython.com 得到了这个代码,这是一个“输入颜色而不是单词”的游戏。
I am using this code to build an improved version of this game and something is wrong and I can't figure why.我正在使用此代码来构建此游戏的改进版本,但出了点问题,我不知道为什么。
So, I want to change the label where the words are (named "label"), to something like "Game Over! Your score is bla bla bla" when the countdown hits 0. So, i did this (what I added is just the 2 last lines):所以,当倒计时达到 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)
This is not working.这是行不通的。 When the countdown hits 0 the game just does nothing and stops.
当倒计时达到 0 时,游戏什么都不做并停止。
I was thinking if I can do this with a while loop...我在想我是否可以用一个 while 循环来做到这一点......
See this answer for more details.有关更多详细信息,请参阅此答案。
You can change the text value of a Label widget 'dynamically' using its textvariable
option with a StringVar
object, or with the .configure()
method of the Label
object.您可以使用
StringVar
对象的textvariable
选项或Label
对象的.configure()
方法“动态”更改 Label 小部件的文本值。 As mentioned in the answer above, the .configure()
method has the benefit of one less object to track正如上面的回答中提到的,
.configure()
方法的好处是减少了一个要跟踪的对象
With textvariable
and StringVar
:使用
textvariable
和StringVar
:
# 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")
With .configure()
使用
.configure()
# ...
my_label = Label(root, text="Default string")
my_label.pack()
#NB: .config() can also be used
my_label.configure(text="New String")
See effbot.org for more details.有关更多详细信息,请参阅effbot.org 。
Without looking at all your code, I would also recommend checking various other issues listed below for possible cause.在不查看所有代码的情况下,我还建议您检查下面列出的各种其他问题以找出可能的原因。 To extend on your comments (on this post), there may be various reasons why the program doesn't 'work' as expected:
为了扩展您的评论(在这篇文章中),程序没有按预期“工作”的原因可能有多种:
if
block ( if timeleft == 0
) and so the .config
method does not get the chance to update the variableif
块( if timeleft == 0
),因此.config
方法没有机会更新变量timeleft
does reach 0
, but after that iteration, it increments above 0
and re-enters the first if
block ( if timeleft>0
), overriding the .config()
you desire.timeleft
确实达到0
,但该次迭代之后,它增加上述0
和重新进入第一if
块( if timeleft>0
),覆盖.config()
你的愿望。.config()
on your widget and overriding your desired changes.config()
并覆盖您想要的更改 To prevent these things from happening, I highly recommend taking a step back, getting some pen and paper and thinking about the overall design of your application.为了防止这些事情发生,我强烈建议退后一步,拿些笔和纸,考虑一下应用程序的整体设计。 Specifically ask yourself:
具体问问自己:
Also consider drawing a flow-chart for the application, from when the user launches the application to the possible paths they can take before closing, making sure blocks in the flow do not contradict each other.还可以考虑为应用程序绘制流程图,从用户启动应用程序到关闭之前他们可以采取的可能路径,确保流程中的块不会相互矛盾。
Finally, also have a look into the Model-View-Controller architecture (and its variants ) for good application design最后,还可以查看模型-视图-控制器架构(及其变体)以获得良好的应用程序设计
initial label-初始标签-
lbl_selection_value1=Label(root, text="Search Option 1")
lbl_selection_value1.grid(row=0,column=0,padx=1)
updated label-更新标签-
lbl_selection_value1.destroy()
lbl_selection_value1_updated = Label(root, text='New Text')
lbl_selection_value1_updated.grid(row=0, column=0, padx=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.