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

时间:2014-04-15 01:05:25

标签: python tkinter

我一直试图为人们输入他们的名字做一个输入框,作为"注册的一部分"对于我正在做的迷你程序,但结果是输入框根本没有出现。

这段代码令我不安:

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)

1 个答案:

答案 0 :(得分:1)

用于创建窗口小部件的第一个参数是您希望将其分配给的父窗口。所以,这一行:

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

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

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

此外,Toplevel中的网格现在没有意义,并且您的几个小工具将叠加显示,直到您更改它为止。