简体   繁体   中英

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.

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. 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. 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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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