简体   繁体   English

如何在 tkinter ~ python 中放置在网格中的两个小部件之间添加空间?

[英]How to add space between two widgets placed in grid in tkinter ~ python?

a.一个。 Have placed a widget in the row 0 in the grid as shown below.如下图所示,在网格的第 0 行放置了一个小部件。

self.a_button = Button(root, text="A Button")
self.a_button.grid(row=0, column=1)

b.湾。 And tried placing another widget in row 2 inside the grid.并尝试在网格内的第 2 行中放置另一个小部件。

self.b_button = Button(root, text="B Button")
self.b_button.grid(row=2, column=1)

But when I run the program, I don't see any space between the widgets, rather its stacked once after the other.但是当我运行程序时,我看不到小部件之间的任何空间,而是一个接一个地堆叠。

So, how do I program to allow space between two widgets placed in different rows?那么,如何编程以允许放置在不同行中的两个小部件之间存在空间? Share your comments !!分享你的评论!!

When you pack the widget you can use打包小部件时,您可以使用

self.a_button = Button(root, text="A Button") 
self.a_button.grid(row=0, column=1, padx=10, pady=10)

Using padx and pady you can add padding to the outer side of the button and alternatively if you want to increase the size of the button you can add inner padding using ipadx and ipady.使用 padx 和 pady 您可以在按钮的外侧添加填充,或者如果您想增加按钮的大小,您可以使用 ipadx 和 ipady 添加内部填充。

If you want more on the Grid function you can view all the options and uses here .如果您想了解更多关于 Grid 功能的信息,您可以在此处查看所有选项和用途。

I think that you already got the answer, but I will share my solution to have space between two lines which works for me well.我认为您已经得到了答案,但我将分享我的解决方案,以便在两行之间留出空间,这对我很有效。

spacer1 = tk.Label(win, text="")
spacer1.grid(row=4, column=0)

you can have this between to labels or entries as empty space at location row= 4, column= 0 .您可以在标签或条目之间将其作为空白位置row= 4, column= 0 You may want to modify the size of the space by adding pad sizes to spacer1.grid(row=4, column=0, padx= 10, pady= 10) or modify the label like spacer1 = tk.Label(win, text="", font=('Times New Roman, 40)) which ever works for you.您可能希望通过将焊盘大小添加到spacer1.grid(row=4, column=0, padx= 10, pady= 10)来修改空间的大小,或者修改标签,如spacer1 = tk.Label(win, text="", font=('Times New Roman, 40))这对你有用。 The out put will be the space between two rows(3 & 5).输出将是两行(3 和 5)之间的空间。 I hope the solution helps you.我希望解决方案对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM