Hello, guys!

I've written a GUI for a program. It's written like shit, the goal was to get it done fast. Well it didn't exactly work out, so when I refresh the data, every label disappears. Here is the code:

class GUI:

    def __init__(self, master):
        self.master = master
        self.label = Label(master, width=50, height=15)
        self.label.pack()
        self.makeMenus()
        self.showStats()

Then, loads of labels, some with textvariables like this:

def showStats(self):
        stats = loadStats()
        # buys
        Label(
            self.label,
            text='Buys',
            font=BOLDFONT
            ).place(x=40, y=10, anchor=NW)
        Label(self.label, text='Times:').place(x=10, y=50, anchor=NW)
        self.buyTimes = StringVar(self.label)
        Label(
            self.label,
            textvariable=self.buyTimes
            ).place(x=60, y=50, anchor=NW)
        ...

... and here is the code for the refresh method:

def refreshStats(self):
        stats = loadStats()
        self.buyTimes.set(stats['buys'])
        ...

So the problem is, that after refreshing, every label disappears for a second, but when I move the cursor over the ones with textvariables, they reapper, but the others don't.

What should I do?

You are creating Label in the function that looks like will be used repeatedly, looks very strange for me. What happens with those Labels.

Could you give ScreenShot of your program without the problem. Any pseudo code from the design of program? Program have working text based version, or did I get it wrong?

You use the same StringVar() if I understand what you want to do. Take a look at the following code and note how the StringVar() is declared and then attached to the second Label() and the Entry() box. Note that when the Entry() box changes the second Label() changes because they both use the same StringVar(), so no refreshing is required.

import Tkinter

class EntryTest:
   """ shows using the same StringVar in the second list box
       and in the entry box
   """
   def __init__(self):
      self.top = Tkinter.Tk()
      self.top.title("Test of Entry")
      self.top.geometry("200x125+10+10")

      self.str_1 = Tkinter.StringVar()
      label_lit = Tkinter.StringVar()

      label_1 = Tkinter.Label(self.top, textvariable = label_lit )
      label_1.pack()
      label_lit.set( "Test of Label")

      label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )
      label_2.pack()

      entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
      entry_1.pack()
      self.str_1.set( "Entry Initial Value" )

      print_button = Tkinter.Button(self.top, text='PRINT CONTENTS',
                     command=self.getit, bg='blue', fg='white' )
      print_button.pack(fill=Tkinter.X, expand=1)

      exit_button= Tkinter.Button(self.top, text='EXIT',
                   command=self.top.quit, bg='red', fg='white' )
      exit_button.pack(fill=Tkinter.X, expand=1)

      entry_1.focus_set()
      self.top.mainloop()

   ##-----------------------------------------------------------------
   def getit(self) :
      print "getit: variable passed =", self.str_1.get()


##===============================================================
if "__main__" == __name__  :
   ET=EntryTest()
   print "under __main__ =", ET.str_1.get()

You can manually update a label also. This example is from somewhere on the web and should use a class like above, but should show you the technique to be used.

from Tkinter import *
root=Tk()

def changeLabel():
    myString.set("I'm, a-fraid we're fresh out of red Leicester, sir. ")
    
myString=StringVar()
Label(root,textvariable=myString).pack()
myString.set("Well, eh, how about a little red Leicester.")
Button(root,text='Click Me',command=changeLabel).pack()
root.mainloop()

Edited 6 Years Ago by woooee: n/a

To anser both of you:
tonyjv:
showStats method is used only once at initializing. It does have a text version by the way.

woooee:
As you can see, my refresh method is the same as your second example. The first isn't too helpful though, I have to load the data from a file, not from input.

What OS are you on guys? Can the problem be caused by Tkinter not compeletely compatible with Win7? It may sound stupid, but I've never had a problem with it before, only once in the past, which was on Win7 too.

EDIT: I just found the problem... I should have known. It was caused by using self.label. I don't know why it doesn't work with it, but it seems like I have to make self.master the root of every widget. Thanks for help though!

Edited 6 Years Ago by sandorlev: n/a

This question has already been answered. Start a new discussion instead.