In this Python tutorial, let us discuss Python GUI Programming with suitable examples.
Introduction of Python GUI Programming
Python provides various choices for generating graphical user interfaces (GUIs). The most popular GUIs are as the following,
- Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.
- wxPython − This is an open-source Python interface for wxWindows.
- JPython − JPython is a port of Python for Java that gives Python scripts seamless access to Java class libraries on the local machine.
Tkinter programming is the standard GUI library for Python when it combines with python that provides a fast way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task and need to perform the following steps,
- Import the Tkinter module.
- Create the GUI application main window.
- Add one or more of the above-mentioned widgets to the GUI application.
- Enter the main event loop to take action against each event triggered by the user.
Example code to create GUI
#!/usr/bin/python import Tkinter top = Tkinter.Tk() # Code to add widgets will go here... top.mainloop()
The following are some of the standard attributes,
- Relief styles
All Tkinter widgets have access to specific geometry management methods, which have the purpose of organizing widgets throughout the parent widget area. Tkinter exposes the following geometry manager classes: pack, grid, and place.
- pack() Method − organizes widgets in blocks before placing them in the parent widget.
- grid() Method − organizes widgets in a table-like structure in the parent widget.
- place() Method − organizes widgets by placing them in a specific position in the parent widget.