TixComboBox子控件在第一个us上未选择/设置为指定值

2024-05-10 11:15:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个TixExFileSelectDialog,它与程序中的几个Entry对象相关联;该对话框是动态配置的,以便所选文件与正在使用该对话框的Entry中的文本相匹配。但是,第一次打开对话框时,不管使用的是Entry,它只显示模式字符串,即使{}已经有了默认值。但是,如果我取消对话框,然后重新打开它,它将显示正确的字符串。当我设置组合框的selectionvalue选项的任意组合时,以及将组合框的variable选项设置为StringVar时,会发生这种情况。在TixComboBoxs的功能中,我缺少什么东西吗?在

我当前使用的代码(带有一些用于发布的重新格式化/等):

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()

Tags: theselfmasterdefvariableframepack对话框
1条回答
网友
1楼 · 发布于 2024-05-10 11:15:43

事实证明,我一开始没有必要做这些,因为我可以使用askopenfilename()from{a1}来获得我想要的功能,使用当前操作系统的外观和感觉。蒂克斯就这么说。在

(好吧,这并不是我想要的,因为它在Windows上还是有点过时,但已经足够接近了。[在这个问题上,IDLE似乎也在使用它。])

相关问题 更多 >