#!/usr/bin/env python3
from tkinter import *
root = Tk()
class GrowLabel(Label):
def __init__(self, master):
Label.__init__(self, master)
self.counter = 12
self.config(text=str(self.counter), fg="blue", font=("verdana", self.counter, "bold"))
self.pack()
button = Button(self.master, text="Stop", command=self.master.destroy)
button.pack()
def count(self):
self.counter += 1
self.config(text=str(self.counter), fg="blue", font=("verdana", self.counter, "bold"))
self.after(1000, self.count)
label = GrowLabel(root)
label.count()
root.mainloop()
1 Answer
Naming
Nouns are best for class names instead of verbs.
GrowLabel
sounds like an action.
GrowingLabel
would sound like a label that grows.
Related to this,
having a count
method on a GrowingLabel
object doesn't sound natural.
The fact that you implement the growing effect using a count should be an internal detail,
and not revealed to users of the class.
The important point is that the label can grow,
not how it does it.
So, renaming the method to grow()
would be more natural.
Calling super-class methods
Since you are in Python 3,
it would be better to call the super constructor of GrowLabel
like this:
super().__init__(master)
If that doesn't work somehow (I don't have tkinter
to try),
then try this instead:
super(GrowLabel, self).__init__(master)
See more details in this related discussion.
Avoid wildcard imports
Don't use wildcard imports like this:
from tkinter import *
Quoting from PEP8:
Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.
I guess you do this because you're using many classes from this module. A better way would be to do like this:
import tkinter as tk
And then you can prefix the classes you need with tk.
, like this:
root = tk.Tk()
-
\$\begingroup\$ What's the difference between
super().__init__
,super(GrowingLabel).__init__
andsuper(GrowingLabel, self).__init__
? \$\endgroup\$– qedCommented Nov 13, 2014 at 14:16 -
1\$\begingroup\$
super().__init__
is the same (more modern) assuper(GrowingLabel, self).__init__
. I didn't know this until you asked, I updated my post now accordingly. I don't know what issuper(GrowingLabel).__init__
\$\endgroup\$– janosCommented Nov 13, 2014 at 19:21