首页 > 解决方案 > TKinter - 按钮不是连续的?

问题描述

我尝试将这 2 个按钮 ia 放在 Dropbox 下方 -

在此处输入图像描述

但正如您在图片中看到的 - 按钮不在同一行。这是我为输出设置网格的代码的一部分:

myB = Button(root,text="Generate",padx=20,pady=10,command=show)
myB2 = Button(root,text="Reset",padx=20,pady=10,command=reset)

drop.grid(row=0,column=0, columnspan=2)
drop2.grid(row=1,column=0, columnspan=2)
drop3.grid(row=2,column=0, columnspan=2)
myB.grid(row=3,column=0)
myB2.grid(row=4,column=1)

任何想法为什么这不能按预期工作?

标签: pythontkinter

解决方案


为了匹配您的图像,代码需要看起来像这样。

root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)
root.rowconfigure(1, weight = 1)
root.columnconfigure(1, weight = 1)
root.rowconfigure(2, weight = 1)

myB = Button(root, text = "Generate", padx = 20, pady = 10, command = show)
myB2 = Button(root, text = "Reset", padx = 20, pady = 10, command = reset)

# sticky needed for resizing with window
drop.grid(row = 0, column = 0, columnspan = 2, sticky = tk.NSEW)
drop2.grid(row = 1, column = 0, columnspan = 2, sticky = tk.NSEW)
drop3.grid(row = 2, column = 0, columnspan = 2, sticky = tk.NSEW)
# This will make buttons 'float' otherwise un-rem sticky
myB.grid(row = 3, column = 0) #, sticky = tk.NSEW)
myB2.grid(row = 3, column = 1) #, sticky = tk.NSEW)

推荐阅读