[英]How to make Tkinter Checkbutton flash
Mac OSX Sierra Tkinter 8.5上的Python 2.7
我将按照该Tkinter文档进行操作,并尝试使用不同的小部件,但是在使Checkbutton闪烁时会遇到一些困难,其中包含文档中描述的结果。
我已经正确地调用了“ self.newButton”调用“ makeCheckbuttonFlash”并打印了消息,但没有看到对checkbutton的任何更改。
注意:在下面的代码中,我丢失了方法选项卡上的格式-不确定如何解决
import Tkinter as tk
class Server(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self, master)
self.grid(sticky = tk.N + tk.S + tk.E + tk.W)
self.createWidgets()
def createWidgets(self):
top = self.winfo_toplevel()
top.rowconfigure(0, weight = 1)
top.columnconfigure(0, weight = 1)
self.rowconfigure(0, weight = 1)
self.columnconfigure(0, weight = 1)
self.quitButton = tk.Button(self, text = "Quit", command = self.quit)
self.quitButton.grid(row = 0, column = 0, sticky = tk.N + tk.S)
self.newButton = tk.Button(self, text = "New", command = self.makeCheckButtonFlash)
self.newButton.grid(row = 0, column = 1, sticky = tk.N + tk.S)
self.checkButton = tk.Checkbutton(self, text = "Check Button", activeforeground = "red")
self.checkButton.grid(row = 1, column = 0)
def makeCheckButtonFlash(self):
print "makeCheckButtonFlash"
self.checkButton.flash()
app = Server()
app.master.title("Server")
app.mainloop()
如果Mac没有按照自己的意愿实现Flash功能,则可以创建自己的Flash功能。 然后,您还可以控制所有参数,例如要闪烁的2种或更多种颜色,两次闪烁之间的时间延迟,要闪烁多少次等。这是对makeCheckButtonFlash的一种修改,可以执行其中的一些操作。
def makeCheckButtonFlash(self, color='black', times=5):
if self.checkButton['foreground'] == self.checkButton['activeforeground']:
self.checkButton['foreground'] = color
else:
self.checkButton['foreground'] = self.checkButton['activeforeground']
times -= 1
if times:
self.checkButton.after(100, lambda t=times: self.makeCheckButtonFlash(times=t))
else:
self.checkButton['foreground'] = color
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.