Python GUI programming (Tkinter)
Tags: python, beginners
Published: 2020-03-03 12:47:44

<a href="https://python.org">Python</a> 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 <a href="https://gumroad.com/l/ErLc">tkinter course</a>

## Tkinter Programming

Tkinter is Python's standard <a href="https://en.wikipedia.org/wiki/Graphical_user_interface">GUI</a> 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:

```python  
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 (<a href="https://effbot.org/zone/tkinter-geometry.htm">geometry manager</a>) that we produced a control

Example:

```python
#!/usr/bin/python

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

Code executed above, results are as follows:

![tkinter window](https://dev-to-uploads.s3.amazonaws.com/i/ir0wwy1o1gh7zoknbv8e.jpg)

Example 2:

```python
#!/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](https://dev-to-uploads.s3.amazonaws.com/i/8ca2jmnkdmc33lsiq3oj.jpg)

## Tkinter components

Tkinter provides various controls such as <a href="https://pythonbasics.org/tkinter-button/">buttons</a>, <a href="https://pythonbasics.org/tkinter-label/">labels</a> and <a href="https://pythonspot.com/tk-message-box/">message boxes</a>.

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:**
* <a href="https://pythonbasics.org/tkinter/">tkinter tutorial</a>
* <a href="https://www.tcl.tk/">tcl/tk homepage</a>