简体   繁体   English

Python 2.7:更新Tkinter Label小部件内容

[英]Python 2.7: updating Tkinter Label widget content

I'm trying to make my Tkinter Label widget update but, where I thought it was straightforward, now I can't sort it out. 我正在尝试更新Tkinter标签窗口小部件,但是,在我认为这很简单的地方,现在我无法对其进行整理。

My code is: 我的代码是:

import Tkinter as tk
import json, htmllib, formatter, urllib2
from http_dict import http_status_dict
from urllib2 import *
from contextlib import closing  

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master) 
        self.grid() 
        self.createWidgets()

    def createWidgets(self):
        StatusTextVar = tk.StringVar()
        self.EntryText = tk.Entry(self)
        self.GetButton = tk.Button(self, command=self.GetURL)
        self.StatusLabel = tk.Label(self, textvariable=StatusTextVar)

        self.EntryText.grid(row=0, column=0)
        self.GetButton.grid(row=0, column=1, sticky=tk.E)
        self.StatusLabel.grid(row=1, column=0, sticky=tk.W)

    def GetURL(self):
         try:
             self.url_target = ("http://www." + self.EntryText.get())
             self.req = urllib2.urlopen(self.url_target)
             StatusTextVar = "Success"

         except:
             self.StatusTextVar = "Wrong input. Retry"
             pass

app = Application()   
app.mainloop()

I've tried several ways but either the Label won't update, or the interpreter raises errors. 我尝试了几种方法,但是Label不会更新,或者解释器会引发错误。 Note: In the excerpt I deleted as much as code as possible to avoid confusion. 注意:在摘录中,我删除了尽可能多的代码以避免混淆。

You need to use the StringVar set method to change the label text. 您需要使用StringVar set方法来更改标签文本。 Also: 也:

StatusTextVar = "Success"

is not referencing self and will not change any state. 没有引用自己,也不会更改任何状态。

You should first change all StatusTextVar to self.StatusTextVar and then update the set calls: 您应该首先将所有StatusTextVar更改为self.StatusTextVar ,然后更新设置的调用:

self.StatusTextVar = "Success"
self.StatusTextVar = "Wrong input. Retry"

to

self.StatusTextVar.set("Success")
self.StatusTextVar.set("Wrong input. Retry")

Updating all StatusTextVar instances and using the set method, I get: 更新所有StatusTextVar实例并使用set方法,我得到:

import Tkinter as tk
import json, htmllib, formatter, urllib2
from urllib2 import *
from contextlib import closing

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.StatusTextVar = tk.StringVar()
        self.EntryText = tk.Entry(self)
        self.GetButton = tk.Button(self, command=self.GetURL)
        self.StatusLabel = tk.Label(self, textvariable=self.StatusTextVar)

        self.EntryText.grid(row=0, column=0)
        self.GetButton.grid(row=0, column=1, sticky=tk.E)
        self.StatusLabel.grid(row=1, column=0, sticky=tk.W)

    def GetURL(self):
         try:
             self.url_target = ("http://www." + self.EntryText.get())
             self.req = urllib2.urlopen(self.url_target)
             self.StatusTextVar.set("Success")

         except:
             self.StatusTextVar.set("Wrong input. Retry")
             pass

root = tk.Tk()
app = Application(master=root)
app.mainloop()

It works as one would expect. 正如人们所期望的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM