Tkinter 更新状态(禁用):需要 redraw/refresh?
Tkinter update state (disable): need for redraw/refresh?
我无法获得 (tk.)Label 和 (tk.)Checkbutton 的状态,以根据 OptionMenu 的值在视觉上变为禁用和正常。
下面的OptionMenu 上的命令绑定似乎没问题,但是我在updateState() 中使用configure(state=...) 没有看到效果。我应该强制使用一些 "refresh" 或 "repaint"(如果是的话,在下面的部分代码中如何?)还是我遗漏了其他东西?
import Tkinter
from Tkinter import Frame, LabelFrame, Label, Entry, Button, StringVar, OptionMenu, Checkbutton, IntVar, DISABLED, NORMAL
class GeneratorDialog:
# The UI to configure the settings by the user
def __init__(self, root, ctrl):
self.__root = root
self.__ctrl = ctrl
def updateState(self, value, label, entry):
if(value.get()==CONST.FORMAT_PDF): # test works, as the dialog below show in alternance as expected
tkMessageBox.showinfo('Info', message="enabling checkbox")
label.configure(state=NORMAL)
entry.configure(state=NORMAL)
else:
tkMessageBox.showinfo('Info', message="disabling the checkbox")
label.configure(state=DISABLED)
entry.configure(state=DISABLED)
#self.__root.update_idletasks() # how to force "redraw" with grid() manager?
def show(self):
self.__root.title(CONST.APP_NAME)
mainFrame = Frame(self.__root)
mainFrame.grid(sticky='ew')
outputFrame = LabelFrame(mainFrame, text='Output Settings')
outputFrame.grid(column=0, row=1, padx=5, pady=5, sticky='ew')
keepGeneratedScribusFilesLabel = Label(outputFrame, text='Keep Scribus Files:', width=15, anchor='e')
keepGeneratedScribusFilesLabel.grid(column=4, row=2, padx=5, pady=5, sticky='e')
keepGeneratedScribusFilesCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getKeepGeneratedScribusFilesCheckboxVariable(), anchor='w')
keepGeneratedScribusFilesCheckbox.grid(column=5, row=2, padx=5, pady=5, sticky='w')
mergeOutputLabel = Label(outputFrame, text='Merge in Single File:', width=15, anchor='w')
mergeOutputLabel.grid(column=0, row=2, padx=5, pady=5, sticky='w')
mergeOutputCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getMergeOutputCheckboxVariable())
mergeOutputCheckbox.grid(column=1, row=2, padx=5, pady=5, sticky='w')
outputFormatLabel = Label(outputFrame, text='Output Format:', anchor='e')
outputFormatLabel.grid(column=2, row=2, padx=5, pady=5, sticky='e')
outputFormatListBox = OptionMenu(outputFrame, self.__ctrl.getSelectedOutputFormat(), *self.__ctrl.getOutputFormatList(),
command=lambda l=keepGeneratedScribusFilesLabel, c=keepGeneratedScribusFilesCheckbox, v=self.__ctrl.getSelectedOutputFormat(): self.updateState(v, l, c))
outputFormatListBox.grid(column=3, row=2, padx=5, pady=5, sticky='w')
# Buttons to Cancel or to Run the Generator with the given Settings
helpButton = Button(buttonFrame, text='Help', width=10, command=self.__ctrl.helpButtonHandler)
helpButton.grid(column=0, row=0, padx=5, pady=5, sticky='e')
cancelButton = Button(buttonFrame, text='Cancel', width=10, command=self.__ctrl.buttonCancelHandler)
cancelButton.grid(column=1, row=0, padx=5, pady=5, sticky='e')
generateButton = Button(buttonFrame, text='Generate', width=10, command=self.__ctrl.buttonOkHandler)
generateButton.grid(column=2, row=0, padx=5, pady=5, sticky='e')
# Finally show the Generator Dialog
mainFrame.grid()
self.__root.grid()
self.__root.mainloop()
通过使用 class 变量 (self.X) 而不是 lambda 局部变量解决了这个问题。
仍然不知道为什么以前的代码(在 lambda 中有局部变量)不会更新 Tk 元素的状态(也许它创建了它的副本?)...但我找到了一个解决方法。
感谢大家的帮助
我无法获得 (tk.)Label 和 (tk.)Checkbutton 的状态,以根据 OptionMenu 的值在视觉上变为禁用和正常。
下面的OptionMenu 上的命令绑定似乎没问题,但是我在updateState() 中使用configure(state=...) 没有看到效果。我应该强制使用一些 "refresh" 或 "repaint"(如果是的话,在下面的部分代码中如何?)还是我遗漏了其他东西?
import Tkinter
from Tkinter import Frame, LabelFrame, Label, Entry, Button, StringVar, OptionMenu, Checkbutton, IntVar, DISABLED, NORMAL
class GeneratorDialog:
# The UI to configure the settings by the user
def __init__(self, root, ctrl):
self.__root = root
self.__ctrl = ctrl
def updateState(self, value, label, entry):
if(value.get()==CONST.FORMAT_PDF): # test works, as the dialog below show in alternance as expected
tkMessageBox.showinfo('Info', message="enabling checkbox")
label.configure(state=NORMAL)
entry.configure(state=NORMAL)
else:
tkMessageBox.showinfo('Info', message="disabling the checkbox")
label.configure(state=DISABLED)
entry.configure(state=DISABLED)
#self.__root.update_idletasks() # how to force "redraw" with grid() manager?
def show(self):
self.__root.title(CONST.APP_NAME)
mainFrame = Frame(self.__root)
mainFrame.grid(sticky='ew')
outputFrame = LabelFrame(mainFrame, text='Output Settings')
outputFrame.grid(column=0, row=1, padx=5, pady=5, sticky='ew')
keepGeneratedScribusFilesLabel = Label(outputFrame, text='Keep Scribus Files:', width=15, anchor='e')
keepGeneratedScribusFilesLabel.grid(column=4, row=2, padx=5, pady=5, sticky='e')
keepGeneratedScribusFilesCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getKeepGeneratedScribusFilesCheckboxVariable(), anchor='w')
keepGeneratedScribusFilesCheckbox.grid(column=5, row=2, padx=5, pady=5, sticky='w')
mergeOutputLabel = Label(outputFrame, text='Merge in Single File:', width=15, anchor='w')
mergeOutputLabel.grid(column=0, row=2, padx=5, pady=5, sticky='w')
mergeOutputCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getMergeOutputCheckboxVariable())
mergeOutputCheckbox.grid(column=1, row=2, padx=5, pady=5, sticky='w')
outputFormatLabel = Label(outputFrame, text='Output Format:', anchor='e')
outputFormatLabel.grid(column=2, row=2, padx=5, pady=5, sticky='e')
outputFormatListBox = OptionMenu(outputFrame, self.__ctrl.getSelectedOutputFormat(), *self.__ctrl.getOutputFormatList(),
command=lambda l=keepGeneratedScribusFilesLabel, c=keepGeneratedScribusFilesCheckbox, v=self.__ctrl.getSelectedOutputFormat(): self.updateState(v, l, c))
outputFormatListBox.grid(column=3, row=2, padx=5, pady=5, sticky='w')
# Buttons to Cancel or to Run the Generator with the given Settings
helpButton = Button(buttonFrame, text='Help', width=10, command=self.__ctrl.helpButtonHandler)
helpButton.grid(column=0, row=0, padx=5, pady=5, sticky='e')
cancelButton = Button(buttonFrame, text='Cancel', width=10, command=self.__ctrl.buttonCancelHandler)
cancelButton.grid(column=1, row=0, padx=5, pady=5, sticky='e')
generateButton = Button(buttonFrame, text='Generate', width=10, command=self.__ctrl.buttonOkHandler)
generateButton.grid(column=2, row=0, padx=5, pady=5, sticky='e')
# Finally show the Generator Dialog
mainFrame.grid()
self.__root.grid()
self.__root.mainloop()
通过使用 class 变量 (self.X) 而不是 lambda 局部变量解决了这个问题。
仍然不知道为什么以前的代码(在 lambda 中有局部变量)不会更新 Tk 元素的状态(也许它创建了它的副本?)...但我找到了一个解决方法。
感谢大家的帮助