pythontkintertix

TixComboBox subwidget not selecting/being set to specified value on first use


I have a TixExFileSelectDialog that is associated with several Entry objects in my program; the dialog is dynamically configured so that the selected file matches the text in the Entry the dialog is being used for. However, the first time the dialog is opened, regardless of which Entry is used, it only displays the pattern string, even when the Entry already has a default value. However, if I cancel out of the dialog and then reopen it, it displays the proper string. This happens when I set any combination of the combo box's selection and value options (one, the other, and both), as well as when I set the combo box's variable option to a StringVar. Is there something I'm missing in how TixComboBoxs function?

The code I'm currently using (with some reformatting/etc. for posting):

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

Solution

  • As it turns out, I had no need to do any of this in the first place because I can just use askopenfilename() from tkinter.filedialog to get exactly the functionality I desire, using the look-and-feel of the current OS. So much for Tix.

    (Well, it wasn't exactly what I wanted, as the look was still a bit dated on Windows, but it was close enough. [IDLE seems to use it too, for that matter.])