tkinter Python中的按钮位置(网格)

时间:2017-06-16 11:02:29

标签: python button tkinter

嘿伙计我有问题而且我一直在寻找答案,但我似乎无法解决我的问题。

我想将“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() 

2 个答案:

答案 0 :(得分:0)

这是一种快速简单的方法,让您的按钮位于程序的底部。

首先将enter_buttonquit_button移至根窗口。这里不需要框架。

然后添加root.rowconfigure()并将权重1应用于按钮所在的行。使用权重配置行允许该行扩展和放置它所放置的框架或窗口。在这种情况下,我们将按钮放在root中,因此我们配置了root。你可以对一个框架做同样的事情,但这只是添加另一个像这样简单的东西不需要的层。

以下是您可以在程序中替换的代码部分,结果应该是您要查找的内容。注意我将按钮并排放置,但您可以使用相同的方法从上到下完成。

编辑:

我忘了添加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)

答案 1 :(得分:-1)

我认为您可以通过添加额外的帧并将其设置到底部来实现。之后,在该框架上输入 Enter 退出按钮。我修改了你的代码,你可以尝试一下。

#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() 

如果适用,请发表评论:)