Previous topic

gui

Next topic

variable

support

Supporting classes for metagui

exit_with_traceback(error_message)

Exit with a traceback, and print a custom error message.

ensure_type(message, required_type, *objects)

Ensure that the given objects are of the required type. If not, print a message and call exit_with_traceback.

divide_list(items, pieces)

Divide a list of things into several pieces, of roughly equal size.

get_photo_image(filename, width=0, height=0, background='', dither=False)

Get a tk.PhotoImage from a given image file.

filename
Full path to the image file, in any format that ‘convert’ understands
width
Desired image width in pixels
height
Desired image height in pixels
background
An ‘#RRGGBB’ string for the desired background color. Only effective for images with transparency.
dither
True to dither the image. May increase graininess. Smallish images usually look better without dithering.

Width and height may be used to scale the image in different ways:

width == 0, height == 0
Preserve the original image’s size
width > 0, height == 0
Resize to the given width; height automatically adjusts to maintain aspect ratio
width == 0, height > 0
Resize to the given height; width automatically adjusts to maintain aspect ratio
width > 0, height > 0
Resize to exactly the given dimensions
class ScrollList(master=None, items=None, selected=None, **kwargs)

A Listbox with a scrollbar.

Similar to a tk.Listbox, a ScrollList shows a list of items. tk.Variable objects may be associated with both the list of items, and the one that is currently selected.

Create a ScrollList widget.

master
Tkinter widget that will contain the ScrollList
items
ListVar or Python list of items to show in listbox
selected
Tk StringVar to store currently selected choice in

Keyword arguments:

width
Width of the listbox, in characters
height
Height of the listbox, in characters
add(*values)

Add the given values to the end of the list. Calls insert callbacks for each added item.

callback(action, function)

Add a callback function for the given action.

action
May be ‘insert’, ‘remove’, ‘select’, or ‘swap’
function
Callback function to call for the given action

All callback functions except ‘swap’ must take (index, value) parameters; the ‘swap’ callback should accept (index_a, index_b). In all cases, index is 0-based, and -1 is passed for the last item.

delete(first, last=None)

Delete values in a given index range (first, last), not including last itself. If last is None, delete only the item at index first. Calls remove callbacks for each removed item.

get()

Return a list of all entries in the list.

insert(index, *values)

Insert values at a given index. Calls insert callbacks for each added item.

Link to another ScrollList, so they scroll and select in unison.

next_item(event)

Event handler when <Return> is pressed

scroll(*args)

Event handler when the list is scrolled.

select(event)

Event handler when an item in the list is selected.

select_index(index, select_in_linked=True)

Select (highlight) the list item at the given index. Calls select callbacks for the selected item.

set(values)

Set the list values to those given.

set_variable(listvar)

Set the ScrollList to use the given ListVar for its items.

summon_callbacks(action, index, item)

Summon callbacks for the given action, passing index and item to each.

swap(index_a, index_b)

Swap the element at index_a with the one at index_b. Calls swap callbacks for the swapped items.

class DragList(master=None, items=None, selected=None)

A scrollable listbox with drag-and-drop support.

Create a DragList widget.

master
Tkinter widget that will contain the DragList
items
ListVar or Python list of items to show in listbox
selected
Tk StringVar to store currently selected list item
drag(event)

Event handler when an item in the list is dragged.

drop(event)

Event handler when an item in the list is “dropped”.

select(event)

Event handler when an item in the list is selected.

class FontChooser(parent=None)

A widget for choosing an ImageMagick font name.

apply()

Set the selected font. Called by base Dialog when “OK” is pressed.

body(master)

Draw widgets inside the Dialog, and return the widget that should have the initial focus. Called by the Dialog base class constructor.

refresh(index=0, fontname='Helvetica')

Redraw the preview using the current font and size.

render_font(fontname)

Return a tk.PhotoImage preview of the given font.

class PopupScale(parent=None)

A popup widget with a scale for choosing a number.

Create the scale dialog with the given Number control as a parent.

apply()

Set the result to the current scale value. Called when “OK” is pressed.

body(master)

Draw the popup dialog and return the widget that should have the initial focus.

class ConfigWindow(master=None, style=None)

Configuration settings dialog box.

Create and display a configuration window.

inifile
An .ini-style file to load/save settings from
apply()

Apply the selected configuration settings.

body(master)

Draw widgets inside the Dialog, and return the widget that should have the initial focus. Called by the Dialog base class constructor.

get_font()

Return a (family, size, style) tuple for the current font.

refresh(*args)

Refresh the preview to show the current font.

class ScrolledWindow(width, height)

A top-level window with scrollbars.

To use as a container for other widgets, do:

window = ScrolledWindow()
button = tk.Button(window.frame, text="Click me", ...)
entry = tk.Entry(window.frame, ...)
window.draw()

That is, use window.frame as the master of child widgets, instead of window itself. (TODO: Eliminate this requirement.)

draw()

Draw the scrollbars and container frame.

class Style(bgcolor='#ffffff', fgcolor='#d9d9d9', textcolor='#000000', font=('Helvetica', 10, 'normal'), relief='groove')

Generic widget style definitions.

apply(root)

Apply the current style to the given Tkinter root window.

load(filename)

Load style settings from an .ini-formatted config file.

save(filename)

Save the current style settings to an .ini-formatted config file.