A newbie question about using tix

MRAB python at mrabarnett.plus.com
Tue Apr 30 15:46:09 EDT 2019


On 2019-04-30 16:40, David Sumbler wrote:
> Running Ubuntu 18.04, Python 3.6.7, tkinter 8.6
> 
> I am very new to tkinter.  The simple program I am writing requires a
> user file to be selected before it does anything else, so I would like
> a file selection dialog in the main window as soon as the program
> launches.
> 
> Tkinter only has askopenfilename(), but this produces a popup dialog.
> I can get something like what I want by specifying a small Tk() window
> and then calling askopenfilename() so that it covers the root window.
> 
> It's not ideal, though.  From the documentation I thought that the tix
> FileSelectBox would do what I wanted, but I just can't get it to work.
> 
> If I run:
> 
>       from tkinter import *
>       from tkinter.tix import FileSelectBox
>       root = Tk()
>       f = FileSelectBox(root)
> 
> I get the following:
> 
>       Traceback (most recent call last):
>         File "/home/david/bin/Gradient.py", line 4, in <module>
>           f = FileSelectBox(root)
>         File "/usr/lib/python3.6/tkinter/tix.py", line 795, in __init__
>           TixWidget.__init__(self, master, 'tixFileSelectBox', ['options'], cnf, kw)
>         File "/usr/lib/python3.6/tkinter/tix.py", line 311, in __init__
>           self.tk.call(widgetName, self._w, *extra)
>       _tkinter.TclError: invalid command name "tixFileSelectBox"
> 
> I realize that assigning the value of FileSelectBox() isn't going to
> give me a filename: I'm just trying to get the basic syntax right at
> the moment.
> 
> I can't figure out what is wrong though.  Have I have misunderstood how
> it should be called, or is there something missing from my system?
> 
For some reason, tix widgets don't work with normal tkinter widgets, so 
you can't put a tix FileSelectBox on a tkinter.Tk widget.

There is, however, a tix.Tk widget that you can use instead:

import tkinter.tix as tix
root = tix.Tk()
f = tix.FileSelectBox(root)
f.pack()



More information about the Python-list mailing list