Python GUI Programming (Tkinter) Windows interface Development

Source: Internet
Author: User
Tags pack



Python implementation GUI simply can call the Tkinter library, so the general requirements can be implemented, showing the simple Windows window code is as follows:



python_gui.py


 1 #!C:\Python27\python.exe
 2 # -*- coding:UTF-8 -*-
 3 
 4 import Tkinter
 5 
 6 def center_window(w = 300, h = 200):
 7     ws = root.winfo_screenwidth()
 8     hs = root.winfo_screenheight()
 9     x = (ws/2) - (w/2)
10     y = (hs/2) - (h/2)
11     root.geometry("%dx%d+%d+%d" % (w, h, x, y))
12 
13 root = Tkinter.Tk(className=‘python gui‘)
14 center_window(500, 300)
15 root.mainloop()


The first line is implemented, directly enter the file name can be executed, do not manually specify the location of the Python.exe is also possible, can now run cmd to execute






As you can see, cmd can start the Python GUI program normally, turn off the GUI interface or turn off the command Line window, the program will automatically end, if we write a program for ordinary users to do, on the one hand, we should copy the Python installation directory in the past, Because there is no Python environment on the general user's computer, we can copy the installation directory directly without the registry support, and on the other hand, the Python GUI program will be accompanied by a black command-line window after the run, which is always not very good-looking, we can consider hiding, hiding the method as follows:



First write the bat script to start the Python source program, script name: Start.bat


1 @echo off
2 start /b C:\Python27\python.exe python_gui.py
3 exit


Of course, the second line start position we can write according to the actual situation, so that directly run Start.bat will start the Python program but will accompany the command-line window, so we have to use a low method, is to use the VBS script to put the program in the background to execute, write script Start.vbs


Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c start.bat",vbhide


By running this VBS script can only see the GUI window, and the cmd window is hidden, here, we want to implement the function, and finally can be compiled in C language into an EXE executable file to execute the above code to implement the call, You can also write a final startup script to start the VBS, such as Run.bat


@echo off
start start.vbs


Although the procedure is called several times, but the speed is almost unaffected, and is used by the client, so there is no problem, this is the simple Python GUI program call method, and the call will let the accompanying command line in the background to execute





Python GUI Programming (Tkinter)


Python provides a library of multiple graphical development interfaces, several common Python GUI libraries are as follows:


    • Tkinter: The 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.

    • Jython: The 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 programming


Tkinter is the standard GUI library for Python. Python uses Tkinter to quickly create GUI applications.



Since Tkinter is built into Python's installation package, as long as Python is installed to import the Tkinter library, and IDLE is also written in Tkinter, for a simple graphical interface Tkinter can handle it.


Note : The python3.x version uses a library named Tkinter, that is, the first letter T is lowercase.

Import 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:


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

The import Tkinter
Top = Tkinter. Tk ()
Enter message loop
Top. Mainloop () 


The above code executes results such as:






Example 2:


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

Import the Tkinter library from Tkinter import * #
Root = Tk() # creates the background color of the window object
Create two lists
Li = [' C ', 'python', 'PHP', 'HTML', 'SQL', 'Java']
Movie = [' CSS ', 'jQuery', 'Bootstrap']
Listb = Listbox(root) # create two list components
Listb2 = Listbox (root)
For item in li: # the first widget inserts the data
By the insert (0, item)

For item in movie: # the second widget inserts the data
Listb2. Insert (0, item)

Listb.pack () # places the widget in the main window
Listb2. Pack ()
Root.mainloop () # enters the message loop 


The above code executes results such as:





Tkinter components


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 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








Http://www.cnblogs.com/freeweb/p/5205119.html






Python GUI Programming (Tkinter) Windows interface development


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.