Python GUI Programming (Tkinter)

Source: Internet
Author: User

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

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.
1 # !/usr/bin/env python 2 # Coding:utf-8 3 4  from Import # Import Tkinter Library 5 6 root=tk ()  # Create window 7 root.mainloop ()  # Start Event loop 

Run under the Python2

Change window

Defining headings

Add a button and a label (label)

BTN = button (root, text= ' This is a button '), representing the creation of a button, Btn.pack () that puts btn on the main window, pack is a way of layout

Label can be used to set the text by using the Config method.

Diary Small Program

Savebtn.pack (Side=left, anchor= ' SW ') means to set the button to the left, side has 4 values, top, BOTTOM, left, right, default to top

Anchor is the alignment, SW is Southwest (southwest), that is, the lower left, and so on, a total of 9 values N, S, W, E, NW, SW, SE, NE, center, the default is the center

When you first read the diary, you need to use Entry and text,entry as a simple input control, text is used to display multiple lines of text.

Stringvar is a string variable type, textvariable represents the value in the text box, write Textvariable=textvar is to facilitate our later on the title of some operations

When you look at the diary, you need to display a list, which will use the ListBox

One more step than the other controls, but it is also very simple, the default list height is too small, so with height=300 set a bit of height

But when the list is empty, we need to have a data source, a variable, a data insert into the list

Improve the function of diary

The first step is to write the controls we want to use, where entry, text, and ListBox are not placed on the main window first.

The next step is to define a method for creating a folder and switching the working directory to that folder

Importing OS module, import os

How to add a click event to a button, first read the diary

Command=write means that when you click this button, the Write method is executed, and then we write the Wirte

Python GUI Programming (Tkinter)

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.