Tutorials for adding a graphical interface to a Python program

Source: Internet
Author: User
Python supports third-party libraries for a variety of graphical interfaces, including:

    • Tk
    • WxWidgets
    • Qt
    • Gtk

Wait a minute.

But Python's own library is a tkinter that supports TK, using Tkinter, which can be used directly without installing any packages. This chapter briefly describes how to use Tkinter for GUI programming.
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.
The first GUI program

Using Tkinter is very simple, let's write a GUI version of "Hello, world!".

The first step is to import all the contents of the Tkinter package:

From Tkinter Import *

The second step is to derive a application class from frame, which is the parent container for all widgets:

Class Application (Frame):  def __init__ (self, master=none):    frame.__init__ (self, Master)    Self.pack ()    Self.createwidgets ()  def createwidgets (self):    Self.hellolabel = Label (self, text= ' Hello, world! ')    Self.helloLabel.pack ()    Self.quitbutton = Button (self, text= ' Quit ', command=self.quit)    Self.quitButton.pack ()

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.

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

In the Createwidgets () method, we create a label and a button that triggers self.quit () to exit the program when the button is clicked.

The third step is to instantiate the application and start the message loop:

App = Application () # set window title: App.master.title (' Hello World ') # main message loop: App.mainloop ()

The main thread of the GUI program listens for messages from the operating system and processes each message in turn. Therefore, if message processing is time-consuming, it needs to be handled in a new thread.

To run this GUI program, you can see the following window:

Click the "Quit" button or the "X" of the window to end the program.
Enter text

Let's improve on this GUI program by adding a text box that allows the user to enter text and then click the button to pop up the message dialog box.

From Tkinter import *import tkmessageboxclass application (Frame):  def __init__ (self, master=none):    frame.__ Init__ (self, Master)    Self.pack ()    self.createwidgets ()  def createwidgets (self):    self.nameinput = Entry (self)    self.nameInput.pack ()    Self.alertbutton = Button (self, text= ' Hello ', Command=self.hello)    Self.alertButton.pack ()  def hello (self):    name = Self.nameInput.get () or "World"    Tkmessagebox.showinfo (' Message ', ' Hello,%s '% name)

When the user taps the button, a hello () is triggered, and when the user enters the text via Self.nameInput.get (), the message dialog box pops up using Tkmessagebox.showinfo ().

The results of the program run as follows:

Summary

Python's built-in Tkinter can meet the requirements of basic GUI programs, and if it is a very complex GUI program, it is recommended to write it in natively supported languages and libraries of the operating system.

Source Code Reference: Https://github.com/michaelliao/learn-python/tree/master/gui

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