如何在第二个窗口中创建输入框?

2024-06-28 11:17:32 发布

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

我一直在尝试做一个输入框,让人们输入他们的名字,作为我正在做的一个小程序的“注册”的一部分,但结果是一个输入框根本没有出现。你知道吗

下面是困扰我的代码部分:

def win2(self):
    # this is the child window
    board = Toplevel()
    board.title("Sign up")
    board.focus_set()
    board.grab_set()

    userVar = StringVar()
    userVar.set('Username')

    square1Label = Label(board,textvariable=userVar)
    square1Label.grid(row=0, column=7)

    userEnt=Entry(self)
    userEnt.grid(row=1, column=7)

    s2Var = StringVar()
    s2Var.set('First Name')
    square2Label = Label(board,textvariable=s2Var)
    square2Label.grid(row=1, column=7)

    leaveButton = Button(board, text="Quit", command=board.destroy)
    leaveButton.grid(row=1, column=1, sticky='nw')
    board.wait_window(board)

这是完整的编码,减去输入Tkinter和主循环:

class Application(Frame):
    """GUI Application for making Baakibook"""
    def __init__(self, parent):
        """Initialize the frame"""
        Frame.__init__(self, parent, bg="light blue")
        self.win1()

    # different windows
    def win1(self):
        # this is the main/root window
        signupButton = Button(root, text="Sign up", command=self.win2)
        signupButton.grid(row=9, column=7)
        loginButton = Button(root, text="Log in", command=self.win3)
        loginButton.grid(row=10, column=7)
        leaveButton = Button(root, text="Quit", command=root.destroy)
        leaveButton.grid(row=1, column=1, sticky='nw')
        b1Var = StringVar()
        b2Var = StringVar()

        b1Var.set('b1')
        b2Var.set('b2')
        box1Label = Label(root,textvariable=b1Var,width=12)
        box1Label.grid(row=3, column=2)
        box2Label = Label(root,textvariable=b2Var,width=12)
        box2Label.grid(row=3, column=3)
        root.mainloop()


    def win2(self):
        # this is the child window
        board = Toplevel()
        board.title("Sign up")
        board.focus_set()
        board.grab_set()

        userVar = StringVar()
        userVar.set('Username')

        square1Label = Label(board,textvariable=userVar)
        square1Label.grid(row=0, column=7)

        userEnt=Entry(self)
        userEnt.grid(row=1, column=7)

        s2Var = StringVar()
        s2Var.set('First Name')
        square2Label = Label(board,textvariable=s2Var)
        square2Label.grid(row=1, column=7)

        leaveButton = Button(board, text="Quit", command=board.destroy)
        leaveButton.grid(row=1, column=1, sticky='nw')
        board.wait_window(board)

    def win3(self):
        # this is the child window
        board = Toplevel()
        board.title("Login User")
        board.focus_set()
        board.grab_set()

        s1Var = StringVar()
        s2Var = StringVar()
        s1Var.set("s1")
        s2Var.set("s2")

        square1Label = Label(board,textvariable=s1Var)
        square1Label.grid(row=0, column=7)
        square2Label = Label(board,textvariable=s2Var)
        square2Label.grid(row=0, column=6)

        leaveButton = Button(board, text="Quit", command=board.destroy)
        leaveButton.grid(row=1, column=1, sticky='nw')
        board.wait_window(board)

Tags: selfboardcolumnrootwindowlabelgridrow
1条回答
网友
1楼 · 发布于 2024-06-28 11:17:32

创建小部件时使用的第一个参数是希望将其分配给哪个父级。所以,这一行:

userEnt=Entry(self)
userEnt.grid(row=1, column=7)

__init__中创建的Frame中生成Entry小部件。如果您希望它出现在Toplevel中,请像在该方法中创建的其他小部件一样创建它,即:

userEnt=Entry(board)
userEnt.grid(row=1, column=7)

另外,您在Toplevel中的网格现在没有意义,在您更改它之前,您的一些小部件将显示为堆叠的。你知道吗

相关问题 更多 >