GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialsteacher.com — Create UI using Tkinter in Python Create UI in Python-TkinterModern computer applications are user-friendly. User interaction is not restricted to console-based I/O. They have a more ergonomic graphical user interface (GUI) thanks to high speed processors and powerful graphics hardware. These applications can receive inputs through mouse clicks and can enable the user to choose from alternatives with the help of radio buttons, dropdown lists, and other GUI elements (or widgets). Such applications are developed using one of various graphics libraries available. A graphics library is a software toolkit having a collection of classes that define a functionality of various GUI elements. These graphics libraries are generally written in C/C++. Many of them have been ported to Python in the form of importable modules. Some of them are listed below: Tkinter is the Python port for Tcl-Tk GUI toolkit developed by Fredrik Lundh. This module is bundled with standard distributions of Python for all platforms. PyQtis, the Python interface to Qt, is a very popular cross-platform GUI framework. PyGTK is the module that ports Python to another popular GUI widget toolkit called GTK. WxPython is a Python wrapper around WxWidgets, another cross-platform graphics library. This tutorial explains the use of Tkinter in developing GUI-based Python programs. Basic GUI ApplicationGUI elements and their functionality are defined in the Tkinter module. The following code demonstrates the steps in creating a UI. First of all, import the TKinter module. After importing, setup the application object by calling the ![]() All Tkinter widget classes are inherited from the Widget class. Let's add the most commonly used widgets. ButtonThe button can be created using the Button class. The Button class constructor requires a reference to the main window and to the options. Signature: You can set the following important properties to customize a button:
Example: Button LabelA label can be created in the UI in Python using the Label class. The Label constructor requires the top-level window object and options parameters. Option parameters are similar to the Button object. The following adds a label in the window. Example: Label Here, the label's caption will be displayed in red colour using Helvetica font of 16 point size. EntryThis widget renders a single-line text box for accepting the user input. For multi-line text input use the Text widget. Apart from the properties already mentioned, the Entry class constructor accepts the following:
The following code adds the text field.
The following example creates a window with a button, label and entry field. Example: Create Widgets The above example will create the following window. ![]() Selection WidgetsRadiobutton: This widget displays a toggle button having an ON/OFF state. There may be more than one button, but only one of them will be ON at a given time. Checkbutton: This is also a toggle button. A rectangular check box appears before its caption. Its ON state is displayed by the tick mark in the box which disappears when it is clicked to OFF. Combobox: This class is defined in the ttk module of tkinterpackage. It populates drop down data from a collection data type, such as a tuple or a list as values parameter. Listbox: Unlike Combobox, this widget displays the entire collection of string items. The user can select one or multiple items. The following example demonstrates the window with the selection widgets: Radiobutton, Checkbutton, Listbox and Combobox: Example: Selection Widgets ![]() Event HandlingAn event is a notification received by the application object from various GUI widgets as a result of user interaction. The Application object is always anticipating events as it runs an event listening loop. User's actions include mouse button click or double click, keyboard key pressed while control is inside the text box, certain element gains or goes out of focus etc. Events are expressed as strings in <modifier-type-qualifier> format. Many events are represented just as qualifier. The type defines the class of the event. The following table shows how the Tkinter recognizes different events:
An event should be registered with one or more GUI widgets in the application. If it's not, it will be ignored. In Tkinter, there are two ways to register an event with a widget. First way is by using the Bind() MethodThe Syntax: Widget.bind(event, callback) For example, to invoke the Example: Even Binding The event object is characterized by many properties such as source widget, position coordinates, mouse button number and event type. These can be passed to the callback function if required. Command ParameterEach widget primarily responds to a particular type. For example, Button is a source of the Button event. So, it is by default bound to it. Constructor methods of many widget classes have an optional parameter called command. This command parameter is set to callback the function which will be invoked whenever its bound event occurs. This method is more convenient than the
In the example given below, the application window has two text input fields and another one to display the result. There are two button objects with the captions Add and Subtract. The user is expected to enter the number in the two Entry widgets. Their addition or subtraction is displayed in the third. The first button (Add) is configured using the command parameter. Its value is the Example: The above example creates the following UI. ![]() Thus, you can create the UI using TKinter in Python. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |