Tkinter Button Alignment in Grid

别等时光非礼了梦想. 提交于 2021-02-09 00:45:48

问题


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!


回答1:


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

Result:

enter image description here



来源:https://stackoverflow.com/questions/22559290/tkinter-button-alignment-in-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!