1
\$\begingroup\$
#!/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()
\$\endgroup\$
3
\$\begingroup\$

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()
\$\endgroup\$
  • \$\begingroup\$ What's the difference between super().__init__, super(GrowingLabel).__init__ and super(GrowingLabel, self).__init__? \$\endgroup\$ – qed Nov 13 '14 at 14:16
  • 1
    \$\begingroup\$ super().__init__ is the same (more modern) as super(GrowingLabel, self).__init__. I didn't know this until you asked, I updated my post now accordingly. I don't know what is super(GrowingLabel).__init__ \$\endgroup\$ – Stop ongoing harm to Monica Nov 13 '14 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.