Python3gui Programming Grid Layout

Source: Internet
Author: User
Python3 GUI Programming This article explains:
This article is divided into two parts, a preliminary analysis of grid layout, two, GUI programming often encountered problems in the solution
believe that the above two parts can help most beginners master grid layout and solve some of the problems.
in the future, I will continue to improve this article according to the problems I encounter.
A, grid layout detailed 1. Overview

A grid layout is a layout that describes an interface as a table, and each control occupies a space that consists of cells in a table, for example, several cells can be merged as a control's space of use.
The width of the cell is affected by the size of the control, and when a control is set to occupy one cell space, and the width of the control is greater than the default size of the cell, the row and column of the cell are stretched to fit the size of the control. Example: 1. Layout

2. Code Layout Way

Import Tkinter as tk

root = tk. TK ()

Label1 = tk. Label (root, Text = "Height:")
label1.grid (row = 0, column = 0)
Label2 = tk. Label (root, Text = "Width:")
label2.grid (row = 1, column = 0)

Entry1 = tk. Entry (Root)
entry1.grid (row = 0, column = 1)
Entry2 = tk. Entry (Root)
entry2.grid (row = 1, column = 1)

Checkbutton = tk. Checkbutton (root, Text = "Preserve aspect")
checkbutton.grid (row = 2, column = 0, rowspan = 1, ColumnSpan = 2, sticky =tk. W) #sticky设置控件的对其方位, which is set to West (left West right east)

img = tk. Photoimage (file = "~/image/image_test.png")
ImageView = tk. Label (Root, image= img)
imageview.grid (row = 0, column = 2, rowspan = 2, ColumnSpan = 2)

button1 = Tk. button (root, Text = "Zoom in")
button1.grid (row = 2, column = 2)
button1 = Tk. button (root, Text = "Zoom out")
button1.grid (row = 2, column = 3)

Root.mainloop ()
3. Effect

We can see that a text box control is significantly wider than the label control, but the text box control occupies only one cell, so the column that contains the cell is stretched to the width of the text box.
Note:
Control must call the grid method for layout operations, otherwise it is not visible
Frame of layout frame is a container that creates a frame

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.