Three kinds of placement pack Tkinter member, grid, place

import Tkinter as tk 
window = tk.Tk()
window.title('My Window')
window.geometry('500x300')  
 
#pack
# Usual pack (), according to the majority of Western and arranged in a way, is the default when not specified.
(Window, text = '123nihao Hello', fg = 'red'). Pack (side = 'top') # on tk.Label
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (side = 'bottom') # under
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (side = 'left') # Left
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (side = 'right') # Right
 
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (anchor = 'n') # North effect supra
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (anchor = 's') #-south, supra
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (anchor = 'w') # West
tk.Label (window, text = '123nihao Hello', fg = 'red'). pack (anchor = 'e') # East
 
You can experience these two different manifestations:
for i in range(3):
    tk.Radiobutton (window, text = i) .pack (anchor = 'w') # append rows, the first three lines of the three buttons are sequentially generated by the West
 
for i in range(3):
    tk.Radiobutton (window, text = i) .pack (side = 'left') # append the same row, the middle row are sequentially generated by a push button on the button 3
 
#grid
# Presented in the form of regular grid. The following code example is to create a table of three rows and three columns: Parameters row to row, the column is about Colum, padx cell spacing, the vertical spacing of about pady cells, cell ipadx elements inside the cell spacing, cell ipady * 2 the vertical spacing of the elements within the cell.
for i in range(3):
    for j in range(3):
      l=tk.Label(window,text='123nihao你好',bg='yellow',font=('Arial', 12), width=10, height=2)
       l.grid(row=i, column=j, padx=5, pady=10, ipadx=1, ipady=20)
     
#place
# Used to locate the precise coordinates, the parameter anchor = 'nw' anchor point is represented by the northwest corner.
tk.Label(window, text='Pl', font=('Arial', 20), ).place(x=20, y=100, anchor='nw')
tk.Label(window, text='Pl', font=('Arial', 20), ).place(x=20, y=200, anchor='nw')
tk.Label(window, text='Pl', font=('Arial', 20), ).place(x=80, y=100, anchor='nw')
tk.Label(window, text='Pl', font=('Arial', 20), ).place(x=80, y=200, anchor='nw')
       
window.mainloop ()
 
 
 
note
3 ways to perform the same time do not mix, will lead the program card out, temporarily unclear reasons.
 
3 ways to support two lines and one line:
w = tk.Label (window, text = '123mn Hello', fg = 'red')
w.pack(side='top')
equal:
tk.Label(window,text='123mn你好',fg='red').pack(side='top')
 
but:
tk.Label (window, text = '123mn Hello', fg = 'red'). pack (side = 'top') # on
tk.Label (window, text = '123mn Hello', fg = 'red'). pack (side = 'bottom') # under
Label will be represented as two members.
 
w = tk.Label (window, text = '123mn Hello', fg = 'red')
w.pack(side='top')
w.pack(side='bottom')
But only represents a Label member, taking only the last bottom.
 
w = tk.Label (window, text = '123mn Hello', fg = 'red')
w.pack(side='top')
w = tk.Label (window, text = '123mn Hello', fg = 'red')
w.pack(side='bottom')
2 can be represented write Label member.
 
On the whole, a plurality of line members is easier wording.
 
 
 

Guess you like

Origin www.cnblogs.com/myshuzhimei/p/11764532.html