In Tkinter you can specify how your widgets look like (color, font size, border etc).
The styling options are specified as options to the widgets either at the time of creating the widgets, or later by using the configure()
option.
Now, let’s see how we can apply different colors, fonts, border widths, reliefs, cursors, and bitmap icons to widgets.
Category Archives: GUI with Python and Tkinter
GUI Python with Tkinter – Part 6 – Events and callbacks
Widgets should be responsive to events (pressing of buttons or keys, mouse clicks etc). This requires associating callbacks with specific events. Callbacks are associated with events, which is known as command binding.
Command binding is the simplest way to add functionality to a button.
command = some_callback
The command
is available only for certain widgets.
If we have the following function …
def callback1():
# do something when button is clicked
GUI Python with Tkinter – Part 5 – The place() geometry manager
Although the place()
geometry manager is the least used geometry manager in Tkinter, it allows you to position widgets using the (x,y)
coordinate system.
The most useful options for place()
are:
- Absolute positioning (
x = N
andy = N
) - Relative positioning (
relx
,rely
,relwidth
, andrelheight
).
Other options that are commonly used with place()
include width
and anchor
.
Example:
from tkinter import *
root = Tk()
# Absolute positioning
Button(root, text = "Absolute").place(x = 60, y = 20)
# x - horizontal placement; y - vertical placement
# Relative positioning
Button(root, text = "Relative").place(relx = 0.8, rely = 0.3, relwidth = 0.3, width = 40, anchor = NE)
root.mainloop()
GUI Python with Tkinter – Part 4 – The grid() geometry manager
The grid geometry manager organizes the container frame into a table with rows and columns. Each cell in the table can hold one widget. Widgets can span multiple cells.
Example:
Let’s create a login window using the grid()
geometry manager. I placed all the code in a file named test04.py
.
from tkinter import *
root = Tk()
label1 = Label(root, text = "Username")
label1.grid(row = 0, sticky = W)
label2 = Label(root, text = "Password")
label2.grid(row = 1, sticky = W)
entry1 = Entry(root)
entry1.grid(row = 0, column = 1, sticky = E)
entry2 = Entry(root)
entry2.grid(row = 1, column = 1, sticky = E)
button1 = Button(root, text = "Login")
button1.grid(row = 2, column = 1, sticky = E)
root.mainloop()
GUI Python with Tkinter – Part 3 – The pack() geometry manager
Now we have a bunch of widgets within out main window (in the test.py
file). It looks hideous now. Let’s see how we can arrange them to look better using geometry managers.
There are three Tkinter geometry managers. They are:
pack()
– Simple to use for simple layouts.grid()
– Commonly used geometry manager, table-like layout of management features.place()
– Least popular, provides the best control for the absolute positioning of widgets.
GUI Python with Tkinter – Part 2 – The widgets
Widgets are the building blocks of GUI programs. When building a GUI the main question become which components (widgets) should appear in the window?
The syntax that is used to add a widget is as follows:
my_widget = Widget-name(its container window, ** its configuration options)
To exemplify, let’s add two widgets (a label, and a button) to the root window we have created in the part 1. This is my test.py
file:
from tkinter import *
root = Tk()
label = Label(root, text="Label widget")
button = Button(root, text="Button widget")
label.pack()
button.pack()
root.mainloop()
GUI Python with Tkinter – Part 1 – Introduction to Tkinter and root window
Tkinter is a graphical user interface (GUI) library for Python. It is the Python interface to Tk, the GUI toolkit for Tcl/Tk.
Tcl (Tool Command Language) is a scripting language used in embedded applications, testing, prototyping, and GUI development. Tk is an open source, multi-platform widget toolkit for building GUI programs.
There are a few key concepts you must understand in order to be able to work with Tkinter and Python.
- Understand the root window and the main loop
- Understand widgets and how to use them (buttons, entry fields, checkboxes, radio buttons, scroll bars etc.
- Understand geometry managers for creating layouts (the position and the structural layout of the components.
- Understand the events and callbacks (interaction and behavior of the components;
command
andevent
bidings). - Understand how to style widgets