#!/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()发布于 2014-11-13 07:59:30
名词最适合类名而不是动词。GrowLabel听起来像是一个动作。GrowingLabel听起来就像一个正在成长的标签。
与此相关,在count对象上使用GrowingLabel方法听起来不太自然。使用计数实现不断增长的效果这一事实应该是内部细节,而不是向类的用户透露。最重要的一点是标签能够增长,而不是它是如何实现的。因此,将该方法重命名为grow()将更加自然。
既然您在Python3中,最好像这样调用GrowLabel的超级构造函数:
super().__init__(master)如果这在某种程度上不起作用(我没有tkinter可以尝试),那么尝试如下:
super(GrowLabel, self).__init__(master)请参阅此相关讨论中的详细信息。
不要像这样使用通配符导入:
从tkinter进口*
引用PEP8的话:
应该避免(从import *)进行通配符导入,因为它们使名称空间中的哪些名称不清楚,从而混淆了读取器和许多自动工具。
我想您这么做是因为您使用了这个模块中的许多类。更好的办法是这样做:
import tkinter as tk然后,您可以用tk.作为类的前缀,如下所示:
root = tk.Tk()https://codereview.stackexchange.com/questions/69670
复制相似问题