DEV Community

Cover image for Python GUI programming (Tkinter)
Python64
Python64

Posted on

Python GUI programming (Tkinter)

Python provides several libraries to develop graphical interface, on of them is tkinter.

Tkinter: Tkinter module ( "Tk Interface") is an interface standard. Python Tk GUI toolkit and Tkinter can be used in most Unix platforms, the same can be applied to both Windows and Macintosh systems.

Versions Tk >= 8.0 can achieve local style window, and run well in the vast majority of platforms.

If you want to learn tkinter, you can with this tkinter course

Tkinter Programming

Tkinter is Python's standard GUI library. With Python using tkinter, you can quickly create GUI applications.

Since python Tkinter is built into the installation package, then as long as the installed Python can import the tkinter library.

Note: Python3.x version of the library named tkinter, namely T lowercase first letter:

import tkinter

Creating a GUI program

  • import module tkinter
  • create the control
  • specify the control master, that is, what the control belongs to
  • tell GM (geometry manager) that we produced a control

Example:

#!/usr/bin/python

import tkinter
top = tkinter.Tk ()
# Enter the message loop
top.mainloop ()

Code executed above, results are as follows:

tkinter window

Example 2:

#!/usr/bin/python
# - * - coding: UTF-8 - * -

from tkinter import * # import Tkinter library
root = Tk () # create window object background color

# Create two lists
li = [ 'C', 'python', 'php', 'html', 'SQL', 'java']
movie = [ 'CSS', 'jQuery', 'Bootstrap']
listb = Listbox (root) # create two lists components
listb2 = Listbox (root)
for item in li: # first small data insertion member
    listb.insert (0, item)

for item in movie: # inserting the second widget data
    listb2.insert (0, item)

listb.pack () # widget is placed into the main window
listb2.pack ()
root.mainloop () # enter the message loop

Code executes above, results are as follows:

tkinter window listbox

Tkinter components

Tkinter provides various controls such as buttons, labels and message boxes.

There are 15 kinds of Tkinter widgets. We describe these components in the following table:

Controls Description
Button button control; display button in the program.
Canvas Canvas controls; displaying graphical elements such as lines or text
Checkbutton checkbox controls; to provide multiple choice box in the program
Entry input controls; for simple text display
Frame Frame control; display a rectangular area on the screen, usually used as containers
Label label control; can display text and bitmaps
Listbox list box control; in Listbox widget is used to display to the user a list of strings
Menubutton menu button control, because the menu items.
Menu menu control; display the menu bar, drop-down menus and pop-up menus
Message Message control; to display multiple lines of text, and more similar label
Radiobutton radio button controls; button to display the status of a radio
Scale range control; displaying a numerical scale, limiting the scope of the digital output section
Scrollbar scroll bar controls, when the content exceeds the viewable area use, such as list boxes. .
Text text control; for multi-line text display
Toplevel container control; to provide a separate dialog boxes, and more similar to Frame
Spinbox input controls; and Entry similar, but you can specify an input range of values
PanedWindow PanedWindow is a window layout management plug-ins, can contain one or more child controls.
LabelFrame labelframe is a simple container control. Common and complex window layout.
tkMessageBox displays a message box for your application.

standard attributes

Standard attribute that is common property of all controls, such as size, font and color, and so on.

Properties Description
Dimension control size;
Color control color;
Font fonts controls;
Anchor anchor;
Relief control style;
Bitmap bitmaps;
Cursor cursor;

  

geometry management

Tkinter control has specific geometric state management methods, management controls the entire regional organizations, it is a geometric Management Tkinter public: bag, grid, location

Geometric methods Description
pack () packing;
grid () grid;
place () position;

Related links:

Top comments (2)

Collapse
 
chonoko_sani profile image
Sani Chonoko Ismail

That is beautiful.

Collapse
 
sativaphoenix profile image
Darryn Dumisani Ph☻enix-92

Thank you!