Turn: Python implements GUI (graphical user interface) programming

Source: Internet
Author: User

Ext: 77971197/

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

    • WxWidgets

    • Qt

    • Gtk

      • The Tkinter:tkinter module (TK interface) is the interface for Python's standard Tk GUI toolkit. 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 native window style and run well on most platforms.

      • Wxpython:wxpython is an open source software, a good set of GUI graphics libraries in the Python language, allowing Python programmers to easily create complete GUI user interfaces with full function keys.

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

Tkinter

Let's comb the concept:

The Python code we write will call the built-in Tkinter,tkinter to encapsulate the interface to the TK;

TK is a graphics library that supports multiple operating systems and is developed using TCL language;

TK will invoke 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, and so on, is a widget. Frame is a widget that can accommodate other widgets, and all widgets are grouped together as a tree.

pack()method to add widgets to the parent container and implement the layout. pack()is the simplest layout that grid() allows for more complex layouts.

Tkinter

Create a GUI program

    • 1. Import Tkinter Module
    • 2. Create a control
    • 3. Specify the master of this control, that is, which one of the controls belongs to
    • 4. Tell GM (geometry Manager) that a control has been generated.

Instance:

#!/usr/bin/python#-*-coding:utf-8-*-importTkinter=Tkinter.  Tk()# enters 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 types of tkinter components. We present these parts as well as a brief introduction in the following table:

Control Description
Button Button control; Displays the button in the program.
Canvas A canvas control; display graphic elements such as lines or text
Checkbutton Multi-box control; for providing multiple selection boxes in a program
Entry Input controls for displaying simple text content
Frame A frame control that displays a rectangular area on the screen and is used more as a container *****************************
Label Label control; can display text and bitmaps
Listbox A list box control; a 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; Displays a single-selected button state
Scale A range control that displays a numeric scale that is a range of numbers for the output limit
Scrollbar A scrollbar 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 A container control that provides a separate dialog box, similar to a frame
Spinbox Input controls, 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 methods Describe
Pack () Packaging
Grid () Grid;
Place () Location



#!/usr/bin/env pythonImport osfrom Time import sleepfrom Tkinter import *class dirlist (object): Def __init__ (self, initdir=none): Sel        F.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 ', font= (' Helvetica ', ' bold ')) Self.dirl.pack () Self.dirfm = Frame (self.top) SELF.DIRSB = Scrollbar (SELF.DIRFM) Self.dirsb.pack (s        Ide=right, fill=y) self.dirs = Listbox (Self.dirfm, height=15, width=50, Yscrollcommand=self.dirsb.set) Self.dirs.bind (' <Double-1> ', Self.setdirandgo) self.dirsb.config (Command=self.dirs.yview) self.d Irs.pack (Side=left, Fill=both) self.dirfm.pack () Self.dirn = Entry (Self.top, width=50, Textvariab        LE=SELF.CWD) self.dirn.bind (' <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, Activ eforeground= ' White ', activebackground= ' green ') Self.quit = Button (SELF.BFM, text= ' quit ', comm         And=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 () if initdir:self . Cwd.set (Os.curdir) self.dols () def clrdir (self, Ev=none): Self.cwd.set (") def setdirandgo (self, Ev=none): Self.last = Self.cwd.get () self.dirs.config (selectbackground= ' red ') Check = Self.dirs.get (s    Elf.dirs.curselection ()) if not Check:check = Os.curdir self.cwd.set (check) self.dols () Def dols (self, ev=none): Error = ' Tdir = Self.cwd.get () if not tdir:tdir = Os.curdir if not Os.path.ex Ists (tdir): Error = Tdir + ': No such file ' Elif not Os.path.isdir (tdir): Error = Tdir + ': n            OT a directory ' if Error:self.cwd.set (Error) Self.top.update () Sleep (2) If not (hasattr (self, ' last ') and self.last): Self.last = Os.curdir SELF.C Wd.set (self.last) self.dirs.config (selectbackground= ' Lightskyblue ') self.top.update ( Return Self.cwd.set (' Fetching DIRECTORY CONTENTS ... ') self.top.update () Dirl ist = Os.listdir (Tdir) Dirlist.sort () Os.chdir (Tdir) Self.dirl.config (TEXT=OS.GETCWD ()) SELF.D Irs.delete (0, end) Self.dirs.insert (end, Os.curdir) Self.dirs.insert (end, Os.pardir) for Eachfile in Dirlist:self. Dirs.insert (END, Eachfile) self.cwd.set (os.curdir) self.dirs.config (selectbackground= ' Lightskyb Lue ') def main (): D = dirlist (Os.curdir) mainloop () if __name__ = = ' __main__ ': Main ()

Goto: Python implementation gui (graphical user interface) programming

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.