Python Tkinter simple layout example tutorial, pythontkinter

Source: Internet
Author: User

Python Tkinter simple layout example tutorial, pythontkinter

This example shows how to implement a simple layout using Python Tkinter. The example provides detailed annotations for readers to understand. Share it with you for your reference. The details are as follows:

#-*-Coding: UTF-8-*-from Tkinter import * root = Tk () #80x80 indicates the size of the main window during initialization, and indicates the location of the window during initialization as root. geometry ('80x80 + 10 + 10') # Fill direction ''' Label (root, text = 'l1', bg = 'red '). pack (fill = Y) Label (root, text = 'l2', bg = 'green '). pack (fill = BOTH) Label (root, text = 'l3 ', bg = 'blue '). pack (fill = X) # Left and Right layout Label (root, text = 'l1', bg = 'red '). pack (fill = Y, side = LEFT) Label (root, text = 'l2', bg = 'green '). pack (fill = BOTH, side = RIGHT) Label (root, text = 'l3 ', bg = 'blue '). pack (fill = X, side = LEFT) # absolute layout l4 = Label (root, text = 'l4 ') l4.place (x = 3, y = 3, anchor = NW) ''' # Grid layout l1 = Label (root, text = 'l1', bg = 'red') l2 = Label (root, text = 'l2 ', bg = 'blue') l3 = Label (root, text = 'l3 ', bg = 'green') l4 = Label (root, text = 'l4 ', bg = 'yellow') l5 = Label (root, text = 'l5 ', bg = 'purple') l1.grid (row = 0, column = 0) l2.grid (row = 1, column = 0) l3.grid (row = 1, column = 1) l4.grid (row = 2) l5.grid (row = 0, column = 3) root. mainloop ()

Shows the Running Effect of Grid layout:

If you are interested, you can test the running effect of the examples in this article. I believe it will be of some reference value for your Python program design.


Python GUI-Tkinter Constructor

Well, what should I do. Every gui program has a window manager called top level to manage those widgets, such as buttons and input boxes. This window manager is the master of these subordinate components, the master of the top-level window is None, that is, it manages itself.
Return to your problem. (1) The master = None in row 4th indicates that the master of the Application class is None by default, that is, it is the top-level window.
(2) The app. master in row 13th is the object that returns the current top-level window, and then calls the title method to set the title. master = None only indicates that the app is the top-level window.

I understand it myself.
Reference: docs.python.org/library/tkinter.html

PYTHON tkinter programming:

The interface design has a basic principle, which is also applicable to tkinter, that is, the interface is single-threaded. Therefore, all events on the interface, especially time-consuming events, cannot be solved in milliseconds. It must be handed over to the background thread. After the background thread completes, it puts the result in a variable, and then the Message notification interface is refreshed normally. In this way, the interface will not get stuck.

The principle of doevents is. Force stop the current thread and process the events in the window message queue. However, sometimes this method does not work well, and it may also lead to a dead end.

So you bound the event, triggered successfully, and executed it. However, the code you run has been running cyclically and does not exit quickly. Therefore, the interface cannot be refreshed.

You can start a thread to do your work.

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.