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