简体   繁体   English

Tkinter / Python网格布局,按钮行跨度问题

[英]Tkinter / Python grid layout, button rowspan issue

i'm trying to make this simple gui using grid layout where i have in one row a label an entry and a button, but for some reason the first button always takes the rowspan equal to the number of rows in previous column, even if i try to force it to have rowspan 1 it has no effect which makes me really confused. 我正在尝试使用网格布局来制作此简单的GUI,其中我在一行中有一个标签,一个条目和一个按钮,但是由于某些原因,第一个按钮的行距总是等于上一列中的行数,即使我尝试强迫它具有rowspan 1,它没有任何作用,这让我真的很困惑。

import tkinter as tk
class MainApplication(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.grid()
        #LABELS
        self.l1 = tk.Label(self, text = "Source")
        self.l1.grid(column = 0, row = 0, sticky = "E")

        self.l2 = tk.Label(self, text = "Text files destination")
        self.l2.grid(column = 0, row = 1, sticky = "E")

        self.l3 = tk.Label(self, text = "Image files destination")
        self.l3.grid(column = 0, row = 2, sticky = "E")
        #ENTRIES
        self.e1 = tk.Entry(self)
        self.e1.grid(column = 1, row = 0)

        self.e2 = tk.Entry(self)
        self.e2.grid(column = 1, row = 1)

        self.e3 = tk.Entry(self)
        self.e3.grid(column = 1, row = 2)
        #BUTTONS

        self.b3 = tk.Button(text = "Select dir", command = self.cb2)
        self.b3.grid(column = 2, row = 0)

        self.b4 = tk.Button(text = "Select dir", command = self.cb2)
        self.b4.grid(column = 2, row = 1)

        self.b5 = tk.Button(text = "Select dir", command = self.cb2)
        self.b5.grid(column = 2, row = 2)


if __name__ == "__main__":
    root = tk.Tk()
    app = MainApplication(root)
    root.mainloop()

output: http://i.imgur.com/AdWkHwi.png 输出: http : //i.imgur.com/AdWkHwi.png

You don't specify a parent for the buttons, so their parent is the root window. 您无需为按钮指定父项,因此它们的父项是根窗口。 The labels and entries, on the other hand, have their parent attribute set to the frame. 另一方面,标签和条目的父属性设置为框架。 What happens is that in the root , the frame is in row zero, and the first button is in row zero, and the height of the row is determined by the height of the frame. 发生的情况是, 在根目录中 ,框架位于第零行,第一个按钮位于第零行,而行的高度由框架的高度确定。

The solution is to make the parent of the buttons be the frame. 解决方案是使按钮的父项成为框架。

    self.b3 = tk.Button(self, ...)
    self.b4 = tk.Button(self, ...)
    self.b5 = tk.Button(self, ...)

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

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