There are a number of graphical user interface (GUI) packages for Python. We've already been using the most popular one, Tkinter so we'll stay with it.
It is pretty easy to create windows in Tkinter and to add widgets (GUI controls) to them. Try the code below to add a label to a window.
Note that the name of tkinter changed from "Tkinter" in Python 2.x to "tkinter" in Python 3.x.
from tkinter import * MasterWindow = Tk() # Get the MasterWindow object from Tkinter TheLabel = Label(MasterWindow, text="Hello, world!") # Add a label to the dialog TheLabel.pack() # Automatically position the item in the dialog MasterWindow.mainloop() # Loop until the user closes the dialog
Note that Tkinter automatically positions the label at the top center of the window and sizes the window to fit the widget.
There are a number of different "widgets" that you can create in Tkinter. You will be familiar with these as a user, now it's time to create them yourself. Try the code below which will put up a series of the basic Tkinter widgets. Try displaying different number of options for popup menus and radio buttons. The "pack()" function automatically places the widgets in a single centered column.
# Add a button to the dialog TheButton = Button(MasterWindow, text="A Button") TheButton.pack() # 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.
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:") TheLabel.place(x=1,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 MasterWindow.mainloop()
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
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, 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 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
Python Documentation: Classes
Tkinter Reference: A GUI for Python
Python Documentation for tkinter
© Copyright 2018 HSU - All rights reserved.