
|
Tkinter Lesson 1 Notes: Graphical User Interfaces
-
Some basics
- Python uses the Tkinter module to make graphical user interfaces
- Before you can make a GUI, you must import Tkinter.
- A GUI can be created interactively in IDLE (and is a lot of fun to watch)
Starting Out
from Tkinter import *
root = Tk()
# creates an instance / object of class Tk
root.title("My Title")
# displays a nice title on the window
root.mainloop()
# keeps displaying the window until we kill it.
- You can name the instance of Tk() anything you want, but "root" seems to be a standard convention for the main window.
- Creating an instance of the Tk() class makes a window.
- The root.mainloop() command is needed for programs not run from within IDLE. If you add this to the end of a program run in IDLE it will hang. If you don’t add this to a program run outside of IDLE, the window will flash on, then disappear.
Short List of Tk() Methods
-
x = Tk()
Creates an object (or instance) of class Tk. This makes your initial "master" window.
x.title('Text here')
Displays a title in the top of the window frame
-
x.mainloop()
Sets up a processing loop for catching changes on the screen
-
x.destroy()
Destroys the window.
Putting Widgets in Windows
- An item inside a window is called a widget
- To add a widget to a window:
- Create a widget (label, button, entry box, etc.)
- For each widget, designate the master window it belongs to.
- Finally, send the widget to a geometry manager to be placed in the window.
- These steps can be done on a single line, but for clarity we will break it up.
-
Creating Widgets
- Widgets are classes.
- Tkinter has a number of defined widget classes, including:
Label, Button, Entry, Menu, Checkbutton, Radiobutton, Message, Text, Canvas, Scrollbar
- To create a new widget, assign an instance to the widget class. The first argument must always be the name of the master window
root = Tk()
widget1 = Label(root)
widget2 = Button(root)
-
The grid() Geometry Manager
- This lesson uses the grid() geometry manager.
- A geometry manager is responsible for arranging the different widgets in a window.
- When grid() is invoked without any arguments, it places each new widget on a new row by itself.
- No widget will appear in the window until it is properly sent to the geometry manager
- To send a widget for grid() to place in the window, type the widget name and then grid()
btn = Button(root, text = “Enter”)
btn.grid()
-
A Label() Widget
-
A Button() Widget
-
The state Option
-
state = NORMAL
- Designates whether the widget is ACTIVE, NORMAL, or DISABLED.
- A DISABLED widget cannot be clicked or edited
- Note that the option is in all caps and is not a string.
-
btn = Button(root, text = "No Way", state = DISABLED)
btn.grid()
An Entry() Widget

from Tkinter import *
root = Tk()
a-label = Label(root, text = "Name: ")
name = Entry(root, width = 30)
# Entry creates a line to type text
# width sets how wide the line is
a-button = Button(root, text = "Enter")
a-label.grid()
name.grid()
a-button.grid()
root.mainloop()
-
Hiding Entry() Text
-
show = "x"
- An option for Entry() that hides what the users types, displaying instead the character you choose.
-
password = Entry(root, show = “*")
password.grid()
-
Standard Widget Options
- Since Label(), Entry(), and Button() are all subclasses of the Widget class, they inherit some common properties.
- The following are options that can be used with any of these widgets.
- To use an option, include it in the () when you first create the object.
Widget Color Options
-
background = 'color'
-
bg = 'color'
-
foreground = 'color'
-
fg = 'color'
-
highlightcolor = 'color'
-
highlightbackground = 'color'
- bg and fg are abbreviations for "background" and "foreground"
- Colors may be entered as keyword names ('red', 'blue', 'gray80', etc.) or hex codes (#FF0000)
- Gray colors can be called as percents. gray80 is 80% gray, which is darker than gray50 (50% gray).
- Color names are not case sensitive
- A list of named colors is here.
-
relief Options
-
relief = RAISED
- Determines type of border around a widget
- Choices are: RAISED, SUNKEN, FLAT, RIDGE, GROOVE, and SOLID
- Note the value uses all caps and is not a string
- In the sample, the relief is applied to a Label() widget
-
Label(root, relief = RAISED)
border Options
-
borderwidth = 2
- Sets the width of the border
- Abbreviated
bd
- In the sample, the border is applied to a Label widget with solid relief.
-
Label (root, relief = SOLID, borderwidth = 2)
-
Label (root, relief = SOLID, bd = 2)
-
justify Option
-
justify = CENTER
- Sets the alignment of text in the widget
- Options are LEFT, RIGHT, and CENTER
- Note the option is in all caps and is not a string.
-
Entry(root, justify = CENTER)
-
font Option
-
font = (‘name’, size, ‘extra’)
- First argument is the name of the font. This argument is not case sensitive.
- Second argument is the font size
- If ‘italic’ or ‘bold’ is desired, enter those words as the last arguments
- If font does not exist, default font will be used.
-
Label(root, text = “Hello”, font = (“Arial”, 12, “bold”))
-
Sample Code:
from Tkinter import *
root = Tk()
root.title("A Rigged Quiz")
q_test = “What is your favorite \n subject at school?”
question = Label(root, text = q_text, font = ('Lucida Handwriting', 14), fg = 'blue')
question.grid()
answer1 = Button(root, text = "Programming")
answer1.grid()
answer2 = Button(root, text = "Math", state = DISABLED)
answer2.grid()
answer3 = Button(root, text = "Science", state = DISABLED)
answer3.grid()
answer4 = Button(root, text = "English", state = DISABLED)
answer4.grid
root.mainloop()
# Do not include the last line if running in IDLE
Restricted access |