我想在每個條目框在tk.Toplevel
實例中已滿時運行命令。Python Tkinter做所有條目滿時的東西
目前,我擁有它,所以每次輸入新的輸入時,它都會檢查它們是否全部已滿。但是,這會變得效率低下,因爲如果輸入一個長字符串,它將檢查每個字母(因爲每次更改條目時都會執行一個條目的command
)。另外,我還有大量的輸入框(總共大約30個)將被使用,所以這將變得非常低效。
are_EntriesFull = lambda master: all(bool(widget.get()) for widget in filter(lambda s: isinstance(s, tk.Entry), findAllWidgets(master)))
_packingWidgets = (tk.Tk, tk.Toplevel, tk.Frame)
def findAllWidgets(master, widgetsFound = None):
'''Returns all of the widgets in a packing instance'''
if widgetsFound is None:
widgetsFound = []
subWidgets = master.grid_slaves()
for widget in subWidgets:
widgetsFound.append(widget)
if isinstance(widget, _packingWidgets):
widgetsFound.extend(findAllWidgets(widget, widgetsFound))
return widgetsFound
class GetWord:
'''Window to get input from the user and create an entry for the word'''
def __init__(self, master, current, total):
self.master, self.current, self.total, self.mainFrame = master, current, total, tk.Frame(master)
self.master.title(STANDARD_TITLE)
self.wordFrame, self.typeFrame, self.infoFrame, self.buttonFrame = tk.Frame(self.mainFrame), tk.Frame(self.mainFrame), tk.Frame(self.mainFrame), tk.Frame(self.mainFrame)
self.word = tk.StringVar(self.wordFrame)
self.wordLabel = tk.Label(self.wordFrame, text = "{}/{}) Word:".format(self.current, self.total), justify = tk.CENTER)
self.activateButton = lambda: self.okButton.configure(state = tk.ACTIVE) if are_EntriesFull(self.master) else None
self.wordEntry = tk.Entry(self.wordFrame, width = 50, justify = tk.CENTER, textvariable = self.word, command = self.activateButton)
# bunch of other code
基本上什麼命令應該做的是激活按鈕(我想這隻有當每個條目被填充使用)。或者,我可以檢查按鈕被按下時的輸入框(並且始終保持激活狀態),並且只有當它們已滿時才執行按鈕的命令,但是我只想在滿足時激活它。 有沒有更好的方法來完成這個?謝謝!
事實證明,我沒有看到正確的信息......而不是'tk.Entry(...,command = self.activateButton)',我不得不使用'self.master.bind(「 「,lambda事件:self.activateButton())',但仍然是相同的想法。不過謝謝! –