2017-06-16 598 views
2

嘿傢伙我有一個問題,我一直在尋找答案,但我似乎無法解決我的問題。tkinter Python中的按鈕位置(網格)

我想將我的「Enter」和「Quit」按鈕更多地移動到窗口底部,但我不確定我是否理解網格功能。

我在窗口內製作了兩個框架,按鈕位於底部框架中,但我似乎無法使用行功能讓它們更低。

我就像剛開始使用Python和有規劃的經驗,所以這是我的第一個項目。(請不要笑)

#import tkinder module 
from tkinter import * 

#make frame 
root = Tk() 
root.geometry("600x300") 

top_frame = Frame(root) 
bottom_frame = Frame(root) 

top_frame.pack() 
bottom_frame.pack() 

#headline 
headline = Label(top_frame, text="Welcome to PrintAssistant.", bg='blue', fg='white') 
headline.config(font=('Courier', 27)) 
headline.grid(padx=10, pady=10) 

Name = Label(bottom_frame, text="Name:", fg='blue') 
Name.config(font=('Courier', 20)) 
Name.grid(row=1) 

Password = Label(bottom_frame, text="Password:", fg='blue') 
Password.config(font=('Courier', 20)) 
Password.grid(row=2) 

Name_entry = Entry(bottom_frame) 
Name_entry.grid(row=1, column=1) 

Password_entry = Entry(bottom_frame) 
Password_entry.grid(row=2, column=1) 

#enter_button 
enter_button = Button(bottom_frame, text="Enter", bg='blue', fg='white') 
enter_button.config(height = 2, width = 15) 
enter_button.grid(sticky = S) 

#quit_button 
quit_button = Button(bottom_frame, text="Quit", bg="blue", fg="white") 
quit_button.config(height = 2, width = 15) 
quit_button.grid(sticky = S) 


root.mainloop() 

回答

0

這裏是一個快速和簡單的方式讓你的按鍵坐在你的程序的底部。

首先將enter_buttonquit_button移動到根窗口。這裏不需要框架。

然後添加root.rowconfigure()並在按鈕所在的行上應用權重1。使用權重配置行允許該行與放置的框架或窗口展開和收縮。在這種情況下,我們將按鈕放在根中,因此我們配置了根。你可以對框架做同樣的事情,但這只是添加另一個不需要的圖層,就像這樣簡單。

以下是您可以在程序中替換的一段代碼,其結果應該是您要查找的內容。注意我並排放置按鈕,但是可以用相同的方法完成從上到下的操作。

編輯:

我忘了添加columnspan部分。您還需要添加其他框架的柱形欄,以便您可以將按鈕放置在居中並排的位置。一個columspan用於告訴程序將有多少個小部件將被佔用。同樣可以用行來完成。

top_frame.grid(row = 0, column = 0, columnspan = 2) 
bottom_frame.grid(row = 1, column = 0, columnspan = 2) 


root.rowconfigure(2, weight = 1) 
#enter_button 
enter_button = Button(root, text="Enter", bg='blue', fg='white') 
enter_button.config(height = 2, width = 15) 
enter_button.grid(row = 2, column=0, sticky = 'se') 

#quit_button 
quit_button = Button(root, text="Quit", bg="blue", fg="white") 
quit_button.config(height = 2, width = 15) 
quit_button.grid(row = 2, column=1, sticky = 'sw') 

如果你想保持退出按鈕頂部的輸入按鈕,那麼你可以做到以下幾點。

top_frame.grid(row = 0, column = 0) # remove columnspan or set it to 1 
bottom_frame.grid(row = 1, column = 0)# remove columnspan or set it to 1 


root.rowconfigure(2, weight = 1) 
root.rowconfigure(3, weight = 0) 
#enter_button 
enter_button = Button(root, text="Enter", bg='blue', fg='white') 
enter_button.config(height = 2, width = 15) 
enter_button.grid(row = 2, column = 0, sticky = 's') 

#quit_button 
quit_button = Button(root, text="Quit", bg="blue", fg="white") 
quit_button.config(height = 2, width = 15) 
quit_button.grid(row = 3, column = 0, sticky = 's') 

如果您只是試圖在輸入欄和按鈕之間放一點空隙,可以使用間隔標籤。類似這樣的:

#empty row spacers 
spacer1 = Label(bottom_frame, text = "") 
spacer1.grid(row = 3) 
spacer2= Label(bottom_frame, text = "") 
spacer2.grid(row = 4) 
#enter_button 
enter_button = Button(bottom_frame, text="Enter", bg='blue', fg='white') 
enter_button.config(height = 2, width = 15) 
enter_button.grid(row = 5, column=1) 
#quit_button 
quit_button = Button(bottom_frame, text="Quit", bg="blue", fg="white") 
quit_button.config(height = 2, width = 15) 
quit_button.grid(row = 6, column=1) 
+0

非常感謝您的幫助!,我嘗試了所有,但是墊片會做! – Spaarwiel

-1

我想你可以通過添加額外的框架做並將其設置在底部。之後,將輸入退出該框架上的按鈕。我修改了你的代碼,你可以試試它。

#import tkinder module 
from tkinter import * 

#make frame 
root = Tk() 
root.geometry("600x300") 

top_frame = Frame(root) 
center_frame = Frame(root) 
bottom_frame = Frame(root) 

top_frame.pack() 
center_frame.pack() 
bottom_frame.pack(side = BOTTOM, fill = BOTH) 

#headline 
headline = Label(top_frame, text="Welcome to PrintAssistant.", bg='blue', fg='white') 
headline.config(font=('Courier', 27)) 
headline.grid(padx=10, pady=10) 

Name = Label(center_frame, text="Name:", fg='blue') 
Name.config(font=('Courier', 20)) 
Name.grid(row=1) 

Password = Label(center_frame, text="Password:", fg='blue') 
Password.config(font=('Courier', 20)) 
Password.grid(row=2) 

Name_entry = Entry(center_frame) 
Name_entry.grid(row=1, column=1) 

Password_entry = Entry(center_frame) 
Password_entry.grid(row=2, column=1) 

#enter_button 
enter_button = Button(bottom_frame, text="Enter", bg='blue', fg='white') 
enter_button.config(height = 2, width = 15) 
enter_button.pack() 

#quit_button 
quit_button = Button(bottom_frame, text="Quit", bg="blue", fg="white") 
quit_button.config(height = 2, width = 15) 
quit_button.pack() 


root.mainloop() 

請發表評論,如果它爲你工作:)

+0

感謝Toru的幫助,一定會記下這一點。 – Spaarwiel