Tkinter status bar?

Daniel T. a at b.c
Wed Sep 25 03:41:36 CEST 2002


I'm going through a pdf file I found on the internet ("An Introduction 
to Tkinter" by Fredrik Lundh)

In chapter 8 he talks about how to put a status bar at the bottom of a 
window:

# File: tkSimpleStatusBar.py
class StatusBar(Frame):
   def __init__(self, master):
      Frame.__init__(self, master)
      self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
      self.label.pack(fill=X)
   
   def set(self, format, *args):
      self.label.config(text=format % args)
      self.label.update_idletasks()
   
   def clear(self):
      self.label.config(text="")
      self.label.update_idletasks()

He goes on to say:

We could have inherited from the Label widget itself, and just extended 
it with set and clear methods. This approach have a few drawbacks, 
though:
* It makes it harder to maintain the status bar's integrity. Some team 
members may cheat, and use config instead of set. That's not a big deal, 
until the day you decide to do some extra processing in the set method. 
Or the day you decide to use a Canvas widget to implement a fancier 
status bar.
* It increases the risk that your additional methods conflict with 
attributes or methods used by Tkinter. While the Frame and Toplevel 
widgets have relatively few methods, other widgets can have several 
dozens of widget specific attributes and methods.
* Future versions of Tkinter may use factory functions rather than class 
constructors for most widgets. However, it's more or less guaranteed 
that such versions will still provide Frame and Toplevel classes. Better 
safe than sorry, in other words.

Now my question. I didn't think to inherit from Label, instead I changed 
it to:

class StatusBar:
   def __init__(self, master):
      self.label = Label(master, text="", bd=1, relief=SUNKEN, anchor=W)
      self.label.pack(side=BOTTOM, fill=X)
 
   def set(self, format, *args):
      self.label.config(text=format % args)
      self.label.update_idletasks()
   
   def clear(self):
      self.label.config(text="")
      self.label.update_idletasks()

In other words, why inherit from any of the Tkinter classes?



More information about the Python-list mailing list