Widget Reference¶
This is a big list of widgets and explanations of what they do. As usual, don’t try to read all of it, unless you’re very bored; it’s best to find what you’re looking for, and ignore rest of this.
-
class
teek.Widget(parent, **kwargs)[source]¶ This is a base class for all widgets.
All widgets inherit from this class, and they have all the attributes and methods documented here.
Don’t create instances of
Widgetyourself likeWidget(...); use one of the classes documented below instead. However, you can useWidgetwithisinstance(); e.g.isinstance(thingy, teek.Widget)returnsTrueifthingyis a teek widget.-
config¶ A dict-like object that represents the widget’s options.
>>> window = teek.Window() >>> label = teek.Label(window, text='Hello World') >>> label.config <a config object, behaves like a dict> >>> label.config['text'] 'Hello World' >>> label.config['text'] = 'New Text' >>> label.config['text'] 'New Text' >>> label.config.update({'text': 'Even newer text'}) >>> label.config['text'] 'Even newer text' >>> import pprint >>> pprint.pprint(dict(label.config)) # prints everything nicely # doctest: +ELLIPSIS {..., 'text': 'Even newer text', ...}
-
state¶ Represents the Ttk state of the widget. The state object behaves like a
setof strings. For example,widget.state.add('disabled')makes a widget look like it’s grayed out, andwidget.state.remove('disabled')undoes that. SeeSTATESin ttk_intro(3tk) for more details about states.Note
Only Ttk widgets have states, and this attribute is set to None for non-Ttk widgets. If you don’t know what Ttk is, you should read about it in the teek tutorial. Most teek widgets are ttk widgets, but some aren’t, and that’s mentioned in the documentation of those widgets.
-
tk_class_name¶ Tk’s class name of the widget class, as a string.
This is a class attribute, but it can be accessed from instances as well:
>>> text = teek.Text(teek.Window()) >>> text.tk_class_name 'Text' >>> teek.Text.tk_class_name 'Text'
Note that Tk’s class names are sometimes different from the names of Python classes, and this attribute can also be None in some special cases.
>>> teek.Label.tk_class_name 'TLabel' >>> class AsdLabel(teek.Label): ... pass ... >>> AsdLabel.tk_class_name 'TLabel' >>> print(teek.Window.tk_class_name) None >>> print(teek.Widget.tk_class_name) None
-
command_list¶ A list of command strings from
create_command().Append a command to this if you want the command to be deleted with
delete_command()when the widget is destroyed (with e.g.destroy()).
-
destroy()[source]¶ Delete this widget and all child widgets.
Manual page: destroy(3tk)
Note
Don’t override this in a subclass. In some cases, the widget is destroyed without a call to this method.
>>> class BrokenFunnyLabel(teek.Label): ... def destroy(self): ... print("destroying") ... super().destroy() ... >>> BrokenFunnyLabel(teek.Window()).pack() >>> teek.quit() >>> # nothing was printed!
Use the
<Destroy>event instead:>>> class WorkingFunnyLabel(teek.Label): ... def __init__(self, *args, **kwargs): ... super().__init__(*args, **kwargs) ... self.bind('<Destroy>', self._destroy_callback) ... def _destroy_callback(self): ... print("destroying") ... >>> WorkingFunnyLabel(teek.Window()).pack() >>> teek.quit() destroying
-
focus(*, force=False)[source]¶ Focuses the widget with focus(3tk).
If
force=Trueis given, the-forceoption is used.
-
classmethod
from_tcl(path_string)[source]¶ Creates a widget from a Tcl path name.
In Tcl, widgets are represented as commands, and doing something to the widget invokes the command. Use this method if you know the Tcl command and you would like to have a widget object instead.
This method raises
TypeErrorif it’s called from a differentWidgetsubclass than what the type of thepath_stringwidget is:>>> window = teek.Window() >>> teek.Button.from_tcl(teek.Label(window).to_tcl()) # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: '...' is a Label, not a Button
-
to_tcl()[source]¶ Returns the widget’s Tcl command name. See
from_tcl().
-
winfo_children()[source]¶ Returns a list of child widgets that this widget has.
Manual page: winfo(3tk)
-
winfo_exists()[source]¶ Returns False if the widget has been destroyed. See
destroy().Manual page: winfo(3tk)
-
winfo_height()[source]¶ Calls
winfo height. Returns an integer.Manual page: winfo(3tk)
-
winfo_id()[source]¶ Calls
winfo id. Returns an integer.Manual page: winfo(3tk)
-
winfo_reqheight()[source]¶ Calls
winfo reqheight. Returns an integer.Manual page: winfo(3tk)
-
winfo_reqwidth()[source]¶ Calls
winfo reqwidth. Returns an integer.Manual page: winfo(3tk)
-
winfo_rootx()[source]¶ Calls
winfo rootx. Returns an integer.Manual page: winfo(3tk)
-
winfo_rooty()[source]¶ Calls
winfo rooty. Returns an integer.Manual page: winfo(3tk)
-
winfo_toplevel()[source]¶ Returns the
Toplevelwidget that this widget is in.Manual page: winfo(3tk)
-
winfo_width()[source]¶ Calls
winfo width. Returns an integer.Manual page: winfo(3tk)
-
winfo_x()[source]¶ Calls
winfo x. Returns an integer.Manual page: winfo(3tk)
-
winfo_y()[source]¶ Calls
winfo y. Returns an integer.Manual page: winfo(3tk)
-
-
class
teek.Button(parent, text='', command=None, **kwargs)[source]¶ A widget that runs a callback when it’s clicked.
See examples/button.py for example code.
textcan be given as withLabel. The'command'option is not settable, and its value is aCallbackthat runs with no arguments when the button is clicked. If the command argument is given, it will be treated so that this…button = teek.Button(some_widget, "Click me", do_something)
…does the same thing as this:
button = teek.Button(some_widget, "Click me") button.config['command'].connect(do_something)
See
Callback.connect()documentation if you need to pass arguments to thedo_somethingfunction.Manual page: ttk_button(3tk)
-
invoke()[source]¶ Runs the command callback.
See
pathname invokein ttk_button(3tk) for details.
-
-
class
teek.Canvas See Canvas Widget.
A square-shaped, checkable box with text next to it.
See examples/checkbutton.py for example code.
For convenience,
textandcommandarguments work the same way as withButton.The
'command'option is not settable, and its value is aCallback. By default, it runs withTrueas the only argument when the checkbutton is checked, and withFalsewhen the checkbutton is unchecked. You can passonvalue=False, offvalue=Trueto reverse this if you find it useful for some reason. This also affects the values that end up in the'variable'option (see manual page), which is aBooleanVar.Manual page: ttk_checkbutton(3tk)
Checks or unchecks the checkbutton, updates the variable and runs the command callback.
See
pathname invokein ttk_checkbutton(3tk) for details.
-
class
teek.Combobox(parent, text='', **kwargs)[source]¶ An entry that displays a list of valid values.
This class inherits from
Entry, so it has all the attributes and methods ofEntry, liketextandcursor_pos.Manual page: ttk_combobox(3tk)
-
class
teek.Entry(parent, text='', **kwargs)[source]¶ A widget for asking the user to enter a one-line string.
The
textoption works as withLabel.See also
Use
Labelif you want to display text without letting the user edit it. Entries are also not suitable for text with more than one line; useTextinstead if you want multiple lines.Manual page: ttk_entry(3tk)
-
cursor_pos¶ The integer index of the cursor in the entry, so that
entry.text[:entry.cursor_pos]andentry.text[entry.cursor_pos:]are the text before and after the cursor, respectively.You can set this attribute to move the cursor.
-
text¶ The string of text in the entry widget.
Setting and getting this attribute calls
get,insertanddeletedocumented in ttk_entry(3tk).
-
-
class
teek.Frame(parent, **kwargs)[source]¶ An empty widget. Frames are often used as containers for other widgets.
Manual page: ttk_frame(3tk)
-
class
teek.Label(parent, text='', **kwargs)[source]¶ A widget that displays text.
For convenience, the
textoption can be also given as a positional initialization argument, soteek.Label(parent, "hello")andteek.Label(parent, text="hello")do the same thing.Manual page: ttk_label(3tk)
-
class
teek.LabelFrame(parent, text='', **kwargs)[source]¶ A frame with a visible border line and title text.
For convenience, the
textoption can be given as withLabel.Manual page: ttk_labelframe(3tk)
-
class
teek.Menu See Menu Widget.
-
class
teek.Notebook See Notebook Widget.
-
class
teek.Progressbar(parent, **kwargs)[source]¶ Displays progress of a long-running operation. This is useful if you are running something concurrently and you want to let the user know that something is happening.
The progress bar can be used in two modes. Pass
mode='indeterminate'and callstart()to make the progress bar bounce back and forth forever. If you want to create a progress bar that actually displays progress instead of just letting the user know that something is happening, don’t passmode='indeterminate'; the default ismode='determinate', which does what you want.There’s a
'value'option that can be used to set the progress in determinate mode. A value of 0 means that nothing is done, and 100 means that we are ready. If you do math on a regular basis, that’s all you need to know, but if you are not very good at math, keep reading:Progress Math
If your program does 5 things, and 2 of them are done, you should do this:
progress_bar.config['value'] = (2 / 5) * 100
It works like this:
- The program has done 2 things out of 5; that is, 2/5. That is a division. Its value turns out to be 0.4.
- We want percents. They are numbers between 0 and 100. The
done / totalcalculation gives us a number between 0 and 1; if we have done nothing, we have0 / 5 == 0.0, and if we have done everything, we have5 / 5 == 1.0. If we add* 100, we get0.0 * 100 = 0.0when we haven’t done anything, and1.0 * 100 == 100.0when we have done everything. Awesome!
progress_bar.config['value'] = (done / total) * 100
However, this fails if
total == 0:>>> 1/0 Traceback (most recent call last): ... ZeroDivisionError: division by zero
If we have no work to do and we have done nothing (
0/0), then how many percents of the work is done? It doesn’t make sense. You can handle these cases e.g. like this:if total == 0: # 0/0 things done, make the progress bar grayed out because # there is no progress to indicate progress_bar.state.add('disabled') else: progress_bar.config['value'] = (done / total) * 100
If multiplying by 100 is annoying, you can create the progress bar like this…
progress_bar = teek.Progressbar(parent_widget, maximum=1)
…and then set numbers between 0 and 1 to
progress_bar.config['value']:if total == 0: progress_bar.state.add('disabled') else: progress_bar.config['value'] = done / total
Manual page: ttk_progressbar(3tk)
-
start(interval=50)[source]¶ Makes an indeterminate mode progress bar bounce back and forth.
The progress bar will move by a tiny bit every interval milliseconds. A small interval makes the progress bar look smoother, but don’t make it too small to avoid keeping CPU usage down. The default should be good enough for most things.
-
class
teek.Scrollbar(parent, **kwargs)[source]¶ A widget for scrolling other widgets, like
Text.In order to use a scrollbar, there are two things you need to do:
- Tell a scrollable widget (e.g.
Text) to use the scrollbar. - Tell the scrollbar to scroll the widget.
For example:
import teek window = teek.Window() text = teek.Text(window) text.pack(side='left', fill='both', expand=True) scrollbar = teek.Scrollbar(window) scrollbar.pack(side='left', fill='y') text.config['yscrollcommand'].connect(scrollbar.set) # 1. scrollbar.config['command'].connect(text.yview) # 2. window.on_delete_window.connect(teek.quit) teek.run()
The value of the scrollbar’s
'command'option is aCallbackthat runs when the scrollbar is scrolled. It runs with arguments suitable forText.xview()orText.yview(). SeeSCROLLING COMMANDSin ttk_scrollbar(3tk) for details about the arguments.Manual page: ttk_scrollbar(3tk)
-
get()[source]¶ Return a two-tuple of floats that have been passed to
set().See also
pathName getin ttk_scrollbar(3tk).
-
set(first, last)[source]¶ Set the scrollbar’s position.
See
pathName setin ttk_scrollbar(3tk) for details.
- Tell a scrollable widget (e.g.
-
class
teek.Separator(parent, **kwargs)[source]¶ A horizontal or vertical line, depending on an
orientoption.Create a horizontal separator like this…
separator = teek.Separator(some_widget, orient='horizontal') separator.pack(fill='x') # default is side='top'
…and create a vertical separator like this:
separator = teek.Separator(some_widget, orient='vertical') separator.pack(fill='y', side='left') # can also use side='right'
See examples/separator.py for more example code.
Manual page: ttk_separator(3tk)
-
class
teek.Spinbox(parent, *, command=None, **kwargs)[source]¶ An entry with up and down buttons.
This class inherits from
Entry, so it has all the attributes and methods ofEntry, liketextandcursor_pos.The value of the
'command'option is aCallbackthat is ran with no arguments. If acommandkeyword argument is given, it will be connected to the callback automatically.Manual page: ttk_spinbox(3tk)
-
class
teek.Text See Text Widget.
-
class
teek.Toplevel(title=None, **options)[source]¶ This represents a non-Ttk
toplevelwidget.Usually it’s easiest to use
Windowinstead. It behaves like aToplevelwidget, but it’s actually aToplevelwith aFrameinside it.Manual page: toplevel(3tk)
-
geometry(width=None, height=None, x=None, y=None)¶ Set or get the size and place of the window in pixels.
Tk’s geometries are strings like
'100x200+300+400', but that’s not very pythonic, so this method works with integers and namedtuples instead. This method can be called in a few different ways:- If width and height are given, the window is resized.
- If x and y are given, the window is moved.
- If all arguments are given, the window is resized and moved.
- If no arguments are given, the current geometry is returned as a namedtuple with width, height, x and y fields.
- Calling this method otherwise raises an error.
Examples:
>>> import teek >>> window = teek.Window() >>> window.geometry(300, 200) # resize to 300px wide, 200px high >>> window.geometry(x=0, y=0) # move to upper left corner >>> window.geometry() Geometry(width=300, height=200, x=0, y=0) >>> window.geometry().width 300
See also
wm geometryin wm(3tk).
-
iconphoto(*images, default=False)¶ Calls
wm iconphotodocumented in wm(3tk).Positional arguments should be
Imageobjects. Ifdefault=Trueis given, the-defaultswitch is used; otherwise it isn’t.
-
withdraw()¶ -
iconify()¶ -
deiconify()¶ These attributes and methods correspond to similarly named things in wm(3tk). Note that
wm_stateisstatein the manual page; the teek attribute iswm_stateto make it explicit that it is the wm state, not some other state.All of the attributes are settable, so you can do e.g.
my_toplevel.title = "lol". Here are the types of the attributes:titleandwm_stateare strings.transientis a widget.minsizeandmaxsizeare tuples of two integers.
-
wait_window()¶ Waits until the window is destroyed with
destroy().This method blocks until the window is destroyed, but it can still be called from the event loop; behind the covers, it runs another event loop that makes the GUI not freeze.
See
tkwait windowin tkwait(3tk) for more details.
-
-
class
teek.Window(*args, **kwargs)[source]¶ A convenient widget that represents a Ttk frame inside a toplevel.
Tk’s windows like
Toplevelare not Ttk widgets, and there are no Ttk window widgets. If you add Ttk widgets to Tk windows likeToplevelso that the widgets don’t fill the entire window, your GUI looks messy on some systems, like my linux system with MATE desktop. This is why you should always create a big Ttk frame that fills the window, and then add all widgets into that frame. That’s kind of painful and most people don’t bother with it, but this class does that for you, so you can just create aWindowand add widgets to that.All initialization arguments are passed to
Toplevel.The
configattribute combines options from theFrameand theToplevelso that it usesFrameoptions whenever they are available, andTopleveloptions otherwise. For example,Framehas an option named'width', sosome_window.config['width']uses that, but frames don’t have a'menu'option, sosome_window.config['menu']uses the toplevel’s menu option.There is no manual page for this class because this is purely a teek feature; there is no
windowwidget in Tk.-
toplevel¶ The
Toplevelwidget that the frame is in. TheWindowobject itself has all the attributes and methods of theFrameinside the window, and for convenience, also manyToplevelthings liketitle,withdraw()andon_delete_window.
-
destroy()[source]¶ Destroys the
topleveland the frame in it.This overrides
Widget.destroy().
-