Python big space between Entry and Button

时间:2018-01-23 19:40:42

标签: python tkinter grid

I'm trying to make a login form. If i add a button to my form, when where appears a big space. I tried grid_columnfigure with weight=1 but it sets the x coordinate only a little bit lower (like self.NewProfilePasswordEntry) My Code:

from tkinter import *
from tkinter import ttk


class App:


    def CreateProfileDataInput(self):
        Root = Tk()
        Root.resizable(width=FALSE, height=FALSE)
        Root.geometry("450x500")

        NewProfilePasswordRepeatLabel = Label(Root, text="Passwort wiederholen", font="Tahoma 13").grid(row=3, column=0, padx=(10, 20), pady=(20, 0), sticky=W)
        self.NewProfilePasswordRepeatEntry = ttk.Entry(Root, width=35, show="*").grid(row=3, column=1, pady=(20, 0))

        self.CreateProfileButton = Button(Root, text="Profil erstellen", width=55, height=2, relief="flat", borderwidth=1, bg="#30b1e8", command=lambda:self.CreateProfile()).grid(row=4, column=0, pady=(25, 0))

        Root.mainloop()

App().CreateProfileDataInput()

I want this
But it gives me that (beacuse the button)

1 个答案:

答案 0 :(得分:0)

最简单的方法是按照建议here添加columnspan=2。上面发生的原因是网格列的行长度是针对最长的列计算的,在上述情况下是Button。当您设置columnspan=2时,它会对其进行调整,以使窗口小部件占用两列而不是一列。

from tkinter import *
from tkinter import ttk


class App:


    def CreateProfileDataInput(self):
        Root = Tk()
        Root.resizable(width=FALSE, height=FALSE)
        Root.geometry("450x500")

        NewProfilePasswordRepeatLabel = Label(Root, text="Passwort wiederholen",
                                                    font="Tahoma 13")
        self.NewProfilePasswordRepeatEntry = ttk.Entry(Root, width=35, show="*")
        self.CreateProfileButton = Button(Root, text="Profil erstellen",
                                                width=55, height=2,
                                                relief="flat", borderwidth=1,
                                                bg="#30b1e8")

        NewProfilePasswordRepeatLabel.grid(row=3, column=0, padx=(10, 20),
                                            pady=(20, 0), sticky=W)
        self.NewProfilePasswordRepeatEntry.grid(row=3, column=1, pady=(20, 0))
        self.CreateProfileButton.grid(row=4, column=0, pady=(25, 0), 
                                            columnspan=2)

        Root.mainloop()

App().CreateProfileDataInput()

此外,请注意,我冒昧地将小部件布局调用与小部件创建调用分开,如果没有这种分离,您的变量都是None,这可能会在以后出现问题。