Python 更改标签上的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17125842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the text on a label
提问by editate
I am having trouble with using a key binding to change the value of a label or any parameter. This is my code:
我在使用键绑定更改标签或任何参数的值时遇到问题。这是我的代码:
from tkinter import*
class MyGUI:
def __init__(self):
self.__mainWindow = Tk()
#self.fram1 = Frame(self.__mainWindow)
self.labelText = 'Enter amount to deposit'
self.depositLabel = Label(self.__mainWindow, text = self.labelText)
self.depositEntry = Entry(self.__mainWindow, width = 10)
self.depositEntry.bind('<Return>', self.depositCallBack)
self.depositLabel.pack()
self.depositEntry.pack()
mainloop()
def depositCallBack(self,event):
self.labelText = 'change the value'
print(self.labelText)
myGUI = MyGUI()
When I run this, I click the entrybox and hit enter, hoping that the label will change value to 'change the value'. However, while it does print that text, the label remains unchanged.
当我运行它时,我单击输入框并按回车键,希望标签将值更改为“更改值”。但是,虽然它确实打印了该文本,但标签保持不变。
From looking at other questions on similar problems and issues, I have figured how to work with some of this outside a class, but I'm having some difficulties with doing it inside a class.
通过查看有关类似问题的其他问题,我已经想出了如何在课堂外处理其中的一些问题,但是在课堂内进行时遇到了一些困难。
Also, on a side note, what role does "master" play in tkinter?
另外,附带说明一下,“master”在 tkinter 中扮演什么角色?
采纳答案by falsetru
self.labelText = 'change the value'
Above sentence make labelText to reference 'change the value', but not change depositLabel's text.
上面的句子使 labelText 引用“更改值”,但不更改存款标签的文本。
To change depositLabel's text, use one of following setences:
要更改 depositLabel 的文本,请使用以下设置之一:
self.depositLabel['text'] = 'change the value'
OR
或者
self.depositLabel.config(text='change the value')
回答by Jake Yang
Here is another one, I think. Just for reference. Let's set a variable to be an instantance of class StringVar
这是另一个,我想。仅供参考。让我们设置一个变量作为类 StringVar 的实例
If you program Tk using the Tcl language, you can ask the system to let you know when a variable is changed. The Tk toolkit can use this feature, called tracing, to update certain widgets when an associated variable is modified.
There's no way to track changes to Python variables, but Tkinter allows you to create variable wrappers that can be used wherever Tk can use a traced Tcl variable.
如果您使用 Tcl 语言编程 Tk,您可以要求系统在变量更改时通知您。Tk 工具包可以使用此功能(称为跟踪)在修改关联变量时更新某些小部件。
无法跟踪 Python 变量的更改,但 Tkinter 允许您创建变量包装器,这些包装器可以在 Tk 可以使用跟踪的 Tcl 变量的任何地方使用。
text = StringVar()
self.depositLabel = Label(self.__mainWindow, text = self.labelText, textvariable = text)
^^^^^^^^^^^^^^^^^
def depositCallBack(self,event):
text.set('change the value')
回答by psyFi
You can also define a textvariablewhen creating the Label, and change the textvariable to update the text in the label.
Here's an example:
您还可以textvariable在创建标签时定义一个,并更改文本变量以更新标签中的文本。下面是一个例子:
labelText = Stringvar()
depositLabel = Label(self, textvariable=labelText)
depositLabel.grid()
def updateDepositLabel(txt) # you may have to use *args in some cases
labelText.set(txt)
There's no need to update the text in depositLabelmanually. Tk does that for you.
无需depositLabel手动更新文本。Tk 为您做到这一点。
回答by Harun ERGUL
I made a small tkinter application which is sets the label after button clicked
我做了一个小的 tkinter 应用程序,它在点击按钮后设置标签
#!/usr/bin/env python
from Tkinter import *
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
class Application:
def __init__(self, master):
frame = Frame(master,width=200,height=200)
frame.pack()
self.log_file_btn = Button(frame, text="Select Log File", command=self.selectLogFile,width=25).grid(row=0)
self.image_folder_btn = Button(frame, text="Select Image Folder", command=self.selectImageFile,width=25).grid(row=1)
self.quite_button = Button(frame, text="QUIT", fg="red", command=frame.quit,width=25).grid(row=5)
self.logFilePath =StringVar()
self.imageFilePath = StringVar()
self.labelFolder = Label(frame,textvariable=self.logFilePath).grid(row=0,column=1)
self.labelImageFile = Label(frame,textvariable = self.imageFilePath).grid(row = 1,column=1)
def selectLogFile(self):
filename = askopenfilename()
self.logFilePath.set(filename)
def selectImageFile(self):
imageFolder = askdirectory()
self.imageFilePath.set(imageFolder)
root = Tk()
root.title("Geo Tagging")
root.geometry("600x100")
app = Application(root)
root.mainloop()
回答by Umang Suthar
Use the configmethod to change the value of label:
使用config方法改变label的值:
top = Tk()
l = Label(top)
l.pack()
l.config(text = "Hello World", width = "50" , )
回答by Darren Samora
There are many ways to tackle a problem like this. There are many ways to do this. I'm going to give you the most simple solution to this question I know. When changing the text of a label or any kind of wiget really. I would do it like this.
有很多方法可以解决这样的问题。有很多方法可以做到这一点。我将给你我所知道的这个问题的最简单的解决方案。当真的改变标签或任何种类的wiget的文本时。我会这样做。
Name_Of_Label["text"] = "Your New Text"
So when I apply this knowledge to your code. It would look something like this.
因此,当我将这些知识应用于您的代码时。它看起来像这样。
from tkinter import*
class MyGUI:
def __init__(self):
self.__mainWindow = Tk()
#self.fram1 = Frame(self.__mainWindow)
self.labelText = 'Enter amount to deposit'
self.depositLabel = Label(self.__mainWindow, text = self.labelText)
self.depositEntry = Entry(self.__mainWindow, width = 10)
self.depositEntry.bind('<Return>', self.depositCallBack)
self.depositLabel.pack()
self.depositEntry.pack()
mainloop()
def depositCallBack(self,event):
self.labelText["text"] = 'change the value'
print(self.labelText)
myGUI = MyGUI()
If this helps please let me know!
如果这有帮助,请告诉我!

