大家好,我有一个问题,我一直在寻找答案,但我似乎无法解决我的问题。
我想将“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()
请您参考如下方法:
这是一种将按钮置于程序底部的快速简单的方法。
首先将enter_button 和quit_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)
