【问题标题】:How to make Tkinter Checkbutton flash如何使 Tkinter Checkbutton 闪烁
【发布时间】:2017-06-29 22:12:57
【问题描述】:

Mac OSX Sierra 上的 Python 2.7 Tkinter 8.5

我正在关注这个Tkinter documentation 并尝试不同的小部件,但在使用文档中描述的结果使复选按钮闪烁时遇到了一些困难。

我已经让“self.newButton”正确调用了“makeCheckbuttonFlash”并打印了消息,但没有看到检查按钮有任何变化。

注意:在下面的代码中,我丢失了方法选项卡上的格式 - 不知道如何修复

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()

【问题讨论】:

  • 您可以通过使用空格而不是制表符来解决制表符问题。请花时间修复它。

标签: python macos tkinter


【解决方案1】:

如果 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

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 2022-06-28
    • 2013-09-17
    • 2020-04-29
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多