Python入口和按钮之间有很大的空间

2024-05-04 12:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做个登录表。 如果我在表单中添加一个按钮,当哪里出现一个很大的空格时。 我尝试了grid_columnfigure的权重=1,但是它只设置了一点点的x坐标(比如self.NewProfilePasswordEntry) 我的代码:

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)


Tags: fromimportselffalseapptkintercolumnroot
1条回答
网友
1楼 · 发布于 2024-05-04 12:34:40

最简单的方法是按建议添加columnspan=2,即here。发生上述情况的原因是网格列的行长度是根据列中最长的列计算的,在上面的例子中是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,这在以后可能会有问题。在

相关问题 更多 >