Tkinter / Python网格布局,按钮rowspan问题

时间:2015-09-22 11:13:05

标签: python python-3.x tkinter

我正在尝试使用网格布局制作这个简单的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()

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

1 个答案:

答案 0 :(得分:0)

您没有为按钮指定父级,因此他们的父级是根窗口。另一方面,标签和条目的父属性设置为框架。会发生什么是根中的,框架在行0中,第一个按钮在行0中,行的高度由框架的高度决定。

解决方案是使按钮的父级成为框架。

Application.DisplayAlerts = False, 
Application.ScreenUpdating = False,
Application.Calculation = xlCalculationManual