我有一个TixExFileSelectDialog
,它与程序中的几个Entry
对象相关联;该对话框被动态配置为使得所选择的文件与该对话框正被用于的CCD_ 3中的文本相匹配。但是,第一次打开对话框时,无论使用哪种Entry
,它都只显示模式字符串,即使Entry
已经有默认值。但是,如果我取消对话框,然后重新打开它,它会显示正确的字符串。当我设置组合框的selection
和value
选项的任意组合(一个、另一个和两者)时,以及当我将组合框的"variable
"选项设置为"StringVar
"时,都会发生这种情况。在TixComboBox
的功能方面,我是否缺少一些东西?
我目前使用的代码(有一些重新格式化/等用于发布):
from tkinter.tix import *
opts = {'path': 'C:\'}
class ImportGUI(Frame):
def _setfsd(self, directory='', pattern='*.xls', variable='', selection=None):
"Reconfigures the ExFileSelectDialog to enable reusability."
self.fsd.fsbox.config(directory=directory or opts['path'], # can't use opts['path'] as a default argument, because it could change.
pattern=pattern)
self.fsd.fsbox.file['variable'] = variable
if not variable:
self.fsd.fsbox.file['value'] = selection or pattern # Defaults to the pattern, which is the behavior of a fresh ExFileSelectionBox.
elif selection is not None: # variable exists, but setting selection manually as well
self.fsd.fsbox.file['value'] = selection
def _selectdatafile(self):
self._setfsd(selection='apple1.xls')
self.fsd.popup()
def _selectcodefile(self):
self._setfsd(selection='apple2.xls')
self.fsd.popup()
def createWidgets(self):
self.fsd = ExFileSelectDialog(self.master) # a top-level widget, so giving it the default root as master
self.dfrow = Frame(self)
self.cfrow = Frame(self)
self.dfentry = Entry(self.dfrow)
self.dfentry.pack(side='left')
self.cfentry = Entry(self.cfrow)
self.cfentry.pack(side='left')
self.dfbtn = Button(self.dfrow, text='...', command=self._selectdatafile)
self.dfbtn.pack(side='left')
self.cfbtn = Button(self.cfrow, text='...', command=self._selectcodefile)
self.cfbtn.pack(side='left')
self.dfrow.pack()
self.cfrow.pack()
self.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.tk.eval('package require Tix')
self.createWidgets()
if __name__ == '__main__': # for testing
gui = ImportGUI()
gui.mainloop()
事实证明,我一开始不需要做任何这些,因为我只需要使用tkinter.filedialog
中的askopenfilename()
,就可以使用当前操作系统的外观来获得我想要的功能。Tix就这么多了。
(嗯,这并不是我想要的,因为它在Windows上的外观仍然有点过时,但它已经足够接近了。[DIDLE似乎也使用了它。])