Python implements GUI (graphical user interface) programming __ Programming

Source: Internet
Author: User
Tags pack

Python supports Third-party libraries for a variety of graphical interfaces, including:

WxWidgets

Qt

Gtk

The Tkinter:tkinter module (TK interface) is the interface of the standard TK GUI Toolkit for Python. Tk and Tkinter can be used on most Unix platforms and can also be applied to Windows and Macintosh systems. Subsequent versions of Tk8.0 can be implemented in a local window style and run well on most platforms.

Wxpython:wxpython is an open-source software that is a good GUI graphics library in the Python language, allowing Python programmers to easily create complete, full-featured GUI user interfaces.

Jython:jython programs can be seamlessly integrated with Java. In addition to some standard modules, Jython uses Java modules. Jython has almost all the modules in standard Python that are not dependent on the C language. For example, the Jython user interface will use SWING,AWT or SWT. Jython can be dynamically or statically compiled into Java bytecode.

Tkinter

Let's comb through the concepts:

The Python code we write invokes the built-in tkinter,tkinter that encapsulates the interface to access TK;

TK is a graphics library that supports multiple operating systems, using TCL language development;

TK invokes the local GUI interface provided by the operating system to complete the final GUI.

So, our code just needs to invoke the interface provided by Tkinter.

In the GUI, each button, Label, input box, etc., is a widget. A frame is a widget that can accommodate other widgets, all of which are grouped together as a tree.

The pack () method adds the widget to the parent container and implements the layout. Pack () is the simplest layout, and grid () can achieve more complex layouts.


Tkinter

Create a GUI program 1, import tkinter Module 2, create control 3, specify Master for this control, that is, which 4 is the control, tell GM (geometry Manager) that a control is generated.

Instance:

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

import tkinter Top
= tkinter.tk ()
# Enter the message loop
Top.mainloop ( )
Component

Tkinter provides a variety of controls, such as buttons, labels, and text boxes, used in a GUI application. These controls are often referred to as controls or parts.

There are currently 15 kinds of tkinter components. We present these parts as well as a brief introduction in the following table:

Control Description
Button Button control, and display a button in the program.
Canvas Canvas control; display graphic elements such as lines or text
Checkbutton Multiple-selection box control; used to provide multiple selection boxes in a program
Entry input control; for displaying simple text content
Frame Frame control; Display a rectangular area on the screen, used as a container *****************************
Label Label controls, text and bitmaps can be displayed
Listbox list box control; the listbox widget is used to display a list of strings to the user
Menubutton The menu button control, because the menu item is displayed.
Menu Menu controls, display menu bars, drop-down menus, and pop-up menus
Message Message control, used to display multiple lines of text, similar to label
Radiobutton radio button control; Display a radio button state
Scale Range control; Display a numeric scale, a range of digits for the output limit
Scrollbar A scroll bar control that is used when the content exceeds a visual area, such as a list box ...
Text Text control, for displaying multiple lines of text
TopLevel container control; Used to provide a separate dialog box, similar to frame
Spinbox Input control, similar to entry, but can specify input range values
Panedwindow Panedwindow is a window layout-managed plug-in that can contain one or more child controls.
Labelframe Labelframe is a simple container control. Commonly used with complex window layouts.
Tkmessagebox A message box to display your application.

Standard Properties

Standard properties are common properties of all controls, such as size, font, color, and so on.

Property Describe
Dimension Control size;
Color Control color;
Font control font;
Anchor Anchor Point;
Relief control style;
Bitmap Bitmap
Cursor Cursor

Geometry Management

The Tkinter control has a specific geometry state management method that manages the entire control area organization, which is the Tkinter exposed Geometry Management class: Package, grid, location

Geometrical method Describe
Pack () Packaging
Grid () Grid;
Place () Location




#!/usr/bin/env pythonImportOs fromTimeImportSleep fromTkinterImport*classDirlist (object):def__init__ (self, initdir=none): Self.top = Tk () Self.label = label (Self.top, text=' Directory Lister v1.2 ') Self.label.pack () Self.cwd=stringvar (self.top) self.dirl = label (Self.top, fg=' Blue ', (' Helvetica ', 12,' Bold ') Self.dirl.pack () Self.dirfm = Frame (self.top) SELF.DIRSB = Scrollbar (SELF.DIRFM) self. Dirsb.pack (Side=right, fill=y) self.dirs = Listbox (Self.dirfm, height=15, width=50, Yscrollcommand=sel F.dirsb.set) Self.dirs.bind (' <Double-1> ', Self.setdirandgo) self.dirsb.config (Command=self.dirs.yview) self.dirs.pack (Side=left, Fill=both) Self.dirfm.pack () Self.dirn = Entry (Self.top, width=50, TEXTVARIABLE=SELF.CWD) self.dirn.bi nd' <Return> ', Self.dols) self.dirn.pack () SELF.BFM = Frame (self.top) self.clr = Button (SELF.BFM, text=' Clear ', Command=self.clrdir, activeforeground=' White ', activebackground=' Blue ') Self.ls = Button (SELF.BFM, text=' List Directory ', Command=self.dols, activeforeground=' White ', activebackground=' Green ') Self.quit = Button (SELF.BFM, text=' Quit ', Command=self.top.quit, activeforeground=' White ', activebackground=' Red ') Self.clr.pack (Side=left) self.ls.pack (side=left) self.quit.pack (side=left) self.bfm.pack ()ifInitdir:self.cwd.set (Os.curdir) Self.dols ()defClrdir (
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.