The Python GUI


There are a number of graphical user interface (GUI) packages for Python. The "standard" one is tkinter but it is really outdated so we'll be using "wx".

It is pretty easy to create windows in wx and to add widgets (GUI controls) to them. Try the code below to add a label to a window.

import wx     
TheApp = wx.App()  
TheWindowFrame = wx.Frame(None, -1, '')  

You'll want to control the size and position of the window with the SetDimensions(X,Y,Width,Height) function:

import wx     
TheApp = wx.App()  
TheWindowFrame = wx.Frame(None, -1, '')  

There are a large number of user interface controls or "Widgets" that you can then add to your window. These include buttons, popup menus (combo boxes), check boxes, radio buttons, and others. To start, we need to add a "panel" to the window that the controls will sit within. Then, we can add the button. Add the code below just after the "SetDimension()" function call and see if the button appears.


ThePanel = wx.Panel(TheWindowFrame, wx.ID_ANY) # create the panel and add it to TheWindowFrame
TheButton = wx.Button(ThePanel, wx.ID_ANY, 'My Button', (10, 30)) # Add the button to the panel with a title and dimensions

# Add a text entry widget TheTextEntry = Entry(MasterWindow) TheTextEntry.pack() # Add a popup menu TheSelection = StringVar(MasterWindow) TheSelection.set("one") # default value ThePopupMenu = OptionMenu(MasterWindow, TheSelection, "one", "two", "three") ThePopupMenu.pack() # A slider (scale bar) TheScale= Scale(MasterWindow, from_=0, to=100) TheScale.pack()  # Add a check box TheCheckBox = Checkbutton(MasterWindow, text="A Check Box") TheCheckBox.pack()

See the Introduction to Tkinter web site for more information.

Positioning Widgets

I find that the automatic placement options in most languages do not give satisfactory results. Instead, you can place the widgets yourself using the "place()" function instead of "pack()". You'll also need to set the minimum size of the window to make sure all the widgets are visible. Below is an example to place the label, replace all the place() functions in your code with replace() to make a readable layout.

from Tkinter import *

MasterWindow = Tk() # Get the MasterWindow object from Tkinter

# Add a label to the dialog

TheLabel = Label(MasterWindow, text="Enter a value:"),y=5) # position the label in the upper left

# Loop until the user closes the dialog

MasterWindow.wm_minsize(240,150) # set the minimum size of the window


Making Widgets Do Stuff

The next step is to make our widgets do something. For this, we need to add "commands" to our widgets. A command is simply a call to a function that is involked when the user interacts with the widget. The code below will print out a message when the button is clicked. You can also have your button open a dialog, close a window, reformat a file or just about anything else you can do in Python.

Add the function below at the start of your script:

def ButtonPressed(): # Called when the button is pressed
	print("The button was pressed") 

Then, add the function to the call the create the button and pass it as the parameter "command":

TheButton=Button(MasterWindow, text="OK", command=ButtonPressed) # add the button to the window

Text Entry Boxes

You can also get the value from a text entry box when a button is pressed. Modify the "ButtonPressed()" function to "get" the value from the text entry box when the button is pressed.

def ButtonPressed(): # Called when the button is pressed
        print("TextEntry is "+TheTextEntry.get()) # print the value

Sliders (Scales in Tkinter)

Sliders, or scales in Tkinter, are similar to text boxes. The code below will get the value from the scale bar when the user clicks on a button.

def ButtonPressed(): # Called when the button is pressed
        print("Scale is "+format(TheScale.get())) # print the value


Getting whether a CheckBox (CheckButton in Python) is a little tricky and uses a special global variable to obtain the value. Note that this variable has to be created after the call to "Tk()" to create the window or it will not work. Change the button pressed function to get the value from the global variable. Note that the value from a check box is an integer (0 for off and 1 for on) so we have to convert it to a string.

def ButtonPressed(): # Called when the button is pressed
        print("Checkbox is "+format(TheCheckBoxValue.get())) # print the value

Then, add the global variable and pass it in as the varaiable for the check box to use:

TheCheckBoxValue = IntVar() # this has to go after the call to Tk() (I'm not sure why)

TheCheckBox = Checkbutton(MasterWindow, text="A Check Box",variable=TheCheckBoxValue) 

Popup Menus

Popup menus work in a similar way to check boxes by using a global variable. We've already setup the global so we just need to use it in our button function.

def ButtonPressed(): # Called when the button is pressed
        print("Selection is "+TheSelection.get()) # print the value

Additional Resources:

Python Documentation: Classes

Python Tutorials - a good tutorial for getting started




© Copyright 2018 HSU - All rights reserved.