The cute Python:python in the TK programming

Source: Internet
Author: User
Tags command line wrapper wrappers in python

David Mertz introduces the TK and Tkinter wrappers (the GUI library of Python) using source code samples and detailed instructions. For ease of operation, he demonstrated the GUI porting example of the Txt2html front-end program that was used in many previous articles. Of course, he thinks you'll read his column regularly.

I'd like to introduce you to the simplest way to start GUI programming, which is to use the Scriptics TK and tkinter wrappers. We'll make a lot of comparisons with the curses library mentioned in "Curses programming in Python" in DeveloperWorks. In addition to the difference between curses implementing the text console and TK implementing the GUI, the two libraries have surprisingly similar interfaces. Before you can use any library, you need to have a basic understanding of Windows and event loops, and refer to the widgets available. (Good, good reference and right amount of practice.) )

As with articles on curses, this article only discusses the characteristics of the tkinter itself. Since many Python distributions have tkinter, there may be no need to download support libraries or other Python modules. The resources later in this article point to a collection of several higher-level user interface widget widgets, but you can do many things with Tkinter itself, including building your own advanced widget. Learning the basic Tkinter module will introduce you to the TK mindset, which is still important even if you continue to use a more advanced widget collection.

TK Brief description

TK is the most closely related to Tcl language, and widely used graphics library, Tcl language and TK are developed by John Ousterhout. Although TK appeared as a X11 library in 1991, it was actually ported to every popular GUI since then. (It is similar to the case where Python gradually has a "standard" GUI.) Now, most popular languages and many small languages have TK bindings (Tkinter modules).

Before I begin, I must confess that I am not a small, withered, TK-programming expert. In fact, most of my TK programming experience started about three days before I wrote this article. The three days were not without challenges, but in the end I thought I had a good grasp of tkinter. What I'm going to say here is that the TK and tkinter wrappers are designed to be very good, user-friendly, and just the simplest introduction to GUI programming.

Starting with the test application

We will use txt2html, the wrapper for the file format converter used in many previous columns (see Resources), as a test application. Although txt2html can be run in several ways, the wrappers here are running txt2html from the command line. The application runs as a batch process with command line arguments that indicate the various aspects of the transformation to be performed. (Later, it is a good idea to provide the user with an interactive selection screen option to lead the user to gradually select different conversion options and provide visual feedback for the selected option before performing the actual conversion.) )

Tk_txt2html is based on the top menu with a drop-down menu and a nested submenu. There is a detailed implementation note next to it, which looks like the Curses version discussed in "Curses Programming in Python." While TK can achieve more functionality with less code, it is clear that tk_txt2html and curses_txt2html are very similar. For example, in TK, an attribute such as a menu can be implemented by a built-in Tkinter class without having to write from scratch.

In addition to setting configuration options, the TK wrapper includes a scrolling help box built with the TK Text widget (a "about" box with the message widget) and a History window for TK dynamic geometry management. As with most interactive applications, the wrapper accepts some user input with the Entry widget for TK.

Before we discuss the code further, let's take a look at the applications that are actually running.

Learn basic knowledge

In fact, the Tkinter program only needs to do three things:

The smallest [Tkinter] program

import Tkinter # import the Tkinter module
root = Tkinter.Tk() # create a root window
root.mainloop() # create an event loop

This is a fully effective Tkinter program (don't mind that it's not practical, because it doesn't even manage "Hello World"). The only thing that the program needs to do is create some widget that holds its root window. After this enhancement, the program's root. Mainloop () method invocation can handle all user interactions without further interference from the programmer.

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.