簡體   English   中英

如何使Tkinter Checkbutton閃爍

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM