2017-06-16 230 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