网格中的Tkinter按钮对齐 [英] Tkinter Button Alignment in Grid
问题描述
无论根框架的大小如何,我都试图在框架中的网格上放置两个按钮,该按钮占据了整行.因此,基本上一个按钮占据了行的一半,而另一个占据了另一半.这是我的代码:
I am attempting to fit two buttons on a grid within a frame, that takes up the entire row, no matter the size of the root frame. So essentially one button takes up half of the row, while the other takes the other half. Here's my code:
self.button_frame = tk.Frame(self)
self.button_frame.pack(fill=tk.X, side=tk.BOTTOM)
self.reset_button = tk.Button(self.button_frame, text='Reset')
self.run_button = tk.Button(self.button_frame, text='Run')
self.reset_button.grid(row=0, column=0)
self.run_button.grid(row=0, column=1)
不太确定从这里去哪里.任何建议将不胜感激.谢谢!
Not really sure where to go from here. Any suggestions would be greatly appreciated. Thanks!
推荐答案
使用 columnconfigure
设置列的权重.然后,当窗口伸展时,列也会伸展.给您的按钮 W
和 E
粘滞值,以便当单元格伸展时,按钮也是如此.
Use columnconfigure
to set the weight of your columns. Then, when the window stretches, so will the columns. Give your buttons W
and E
sticky values, so that when the cells stretch, so do the buttons.
import Tkinter as tk
root = tk.Tk()
button_frame = tk.Frame(root)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)
reset_button = tk.Button(button_frame, text='Reset')
run_button = tk.Button(button_frame, text='Run')
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
reset_button.grid(row=0, column=0, sticky=tk.W+tk.E)
run_button.grid(row=0, column=1, sticky=tk.W+tk.E)
root.mainloop()
结果:
这篇关于网格中的Tkinter按钮对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!