0

我有一个与我的程序TixExFileSelectDialog中的几个Entry对象相关联的;该对话框是动态配置的,以便所选文件与Entry正在使用的对话框中的文本匹配。但是,第一次打开对话框时,无论使用哪个Entry,它都只显示模式字符串,即使Entry已经有默认值。但是,如果我取消对话框然后重新打开它,它会显示正确的字符串。当我设置组合框selectionvalue选项(一个、另一个和两者)的任意组合以及将组合框的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()
4

1 回答 1

0

事实证明,我一开始就不需要做任何这些,因为我可以使用askopenfilename()fromtkinter.filedialog来获得我想要的功能,使用当前操作系统的外观。Tix 就这么多。

(嗯,这不是我想要的,因为在 Windows 上看起来仍然有点过时,但它已经足够接近了。[IDLE 似乎也使用它,就此而言。]

于 2012-07-27T18:08:38.263 回答