如何从python tkinter中的当前窗口打开一个新框架?

时间:2017-09-19 18:17:50

标签: python tkinter

class Login:
    def __init__(self):
        Label1 = Label(root,text = "Username")
        Label2 = Label(root,text = "Password")
        self.Entry1 = Entry(root)
        self.Entry2 = Entry(root,show = "*")

        Label1.grid(row=0)
        Label2.grid(row=1)

        self.Entry1.grid(row = 0,column = 1)
        self.Entry2.grid(row = 1,column = 1)

        root.minsize(width = 300,height = 80)
        ##new_window_button = Button(text="new window", command = ????)
        ##new_window_button.grid(columnspan = 2)

        lgbutton = Button(text = "Login",command = self.ButtonClicked)
        lgbutton.grid(columnspan = 2)


    def ButtonClicked(self):
            username = self.Entry1.get()
            password = self.Entry2.get()
            GetDatabase(username,password)

目前我必须创建一个窗口,但是当我点击new_window_button时,我想要它,新页面有自己的小部件。我之前使用过Toplevel但它创建了一个没有小部件的子窗口。而是将小部件添加到父窗口。

2 个答案:

答案 0 :(得分:1)

根据评论判断,看起来你正在努力宣布小部件的正确父级。

当声明一个小部件时,传入它的第一个参数是它的父级。例如:

Label(root, text="I'm in the root window.")
#      ^ This is the parent

相反:

Label(top, text="I'm in the top window.")
#      ^ This is the parent

请参阅下面更加充实的示例:

from tkinter import *

root = Tk()

top = Toplevel(root)

label1 = Label(root, text="I'm a placeholder in your root window.")
label2 = Label(top, text="I'm a placeholder in your top window.")

label1.pack()
label2.pack()

root.mainloop()

答案 1 :(得分:0)

import tkinter

from tkinter import *

class LoginForm(Frame):
 def __init__(self,master=None):
    super().__init__(master)
    self.pack()
    self.createWidget()

 def createWidget(self):
    self.lblEmailId=Label(self,text="Email Id")
    self.lblEmailId.grid(row=0,column=0)
    self.varEmailid=StringVar()
    self.txtEmailId=Entry(self,textvariable=self.varEmailid)
    self.txtEmailId.grid(row=0,column=1)
    self.txtEmailId.bind("<KeyRelease>",self.key_press)

    self.lblPassword = Label(self, text="Password")
    self.lblPassword.grid(row=1, column=0)
    self.varPassword=StringVar()

    self.txtPassword= Entry(self, textvariable=self.varPassword)
    self.txtPassword.grid(row=1, column=1)
    self.btnLogin=Button(self,text="Login")
    self.btnLogin.grid(row=2,column=1)
    self.btnLogin.bind("<Button-1>",self.btnLogin_click)

 def btnLogin_click(self,event):
    self.varPassword.set(self.varEmailid.get())
    LoginWindow=Toplevel()






 def key_press(self,event):
    self.varPassword.set(self.varEmailid.get())


root=Tk()
fromLogin=LoginForm(root)
root.mainloop()