2017-04-15 31 views
-1

所以我想建立一個助理關閉,將做自動備份ups等,而不是使用.place我想要一個適當的網格放置小部件。 我找不到網格管理器的一個很好的例子。tkinter網格管理器行爲

self.parent = tk.Frame(window, bg = BLACK)   
username_label = ttk.Label(self.parent, text = "Username") 
password_label = ttk.Label(self.parent, text = "Password") 

self.parent.grid(column = 0, row = 0) 
username_label.grid(column = 1, row = 1) 
password_label.grid(column = 2, row = 2) 

self.parent.grid_columnconfigure(0, weight = 1) 

我想...

   Button 
       Button 
Label Entry Button 
Label Entry Button 
       Button 

我不明白我怎麼可以定位他們喜歡這個,因爲我想在標籤上面一片空白。到目前爲止,網格只讓我把東西放在一起。

老實說,任何網站或代碼示例將不勝感激

回答

2

所以,如果你想在標籤上面的空格,您可以設置pady作爲參數傳遞給grid方法或者乾脆把它們對應的行。考慮以下示例:

import tkinter as tk 
root=tk.Tk() 

for i in range(6): 
    tk.Button(root,text='Button %d'%i).grid(row=i,column=1) 
tk.Label(root,text='Label 0').grid(row=2,column=0,pady=20) 
tk.Label(root,text='Label 1').grid(row=3,column=0) 

root.mainloop() 

請注意pady參數的影響。另外,如果您只想在Label以上的空白行,您可以嘗試在上面一行中輸入空白Label。例如: -

import tkinter as tk 
root=tk.Tk() 

for i in range(6): 
    tk.Button(root,text='Button %d'%i).grid(row=i,column=1) 
tk.Label(root,text='Label 0').grid(row=2,column=0,pady=20) 
tk.Label(root,text='Label 1').grid(row=3,column=0) 
tk.Label(root,text='').grid(row=6) 
tk.Label(root,text='This is a Label with a blank row above').grid(row=7,columnspan=2) 
root.mainloop() 

您可以參考effbot更多的信息,這是Tkinter的開發者的博客。

+0

絕對的傳說!非常感謝你! –