创建一个多级Tkinter窗口

时间:2019-12-25 19:15:47

标签: python tkinter

Good Day,我正在使用python 3并使用tkinter创建一个多窗口系统。我的目标是:

  • [窗口1]登录屏幕
  • [窗口2]菜单屏幕(此处将消失登录屏幕)
  • [Window 3]另一个将执行 窗口2中选中的菜单

我是tkinter的新手,我无法实现window3,因为每次我尝试从window 2调用window 3时,都会打开额外的空间。我完全糊涂了,死了。如果有人能帮助我,我真的会背上沉重的债务。 预先感谢。

from tkinter import*
import tkinter.messagebox
from tkinter import ttk
import random
import time
import datetime

class Window1:

    def __init__(self, master):
        self.master =master
        self.master.title("Login System")
        # self.master.geometry('1350x750+0+0')
        # self.master.config(bg ='powder blue')
        self.frame = Frame(self.master)
        self.frame.pack()

        self.Username = StringVar()
        self.Password = StringVar()

        self.lblTitle = Label(self.frame, text = 'Login System', font=('arial',25,'bold'))
        self.lblTitle.grid(row=0, column=0, columnspan=2, pady=40)

        self.LoginFrame1 = LabelFrame(self.frame, width=1350, height=600,font=('arial',20,'bold'),relief='ridge')
        self.LoginFrame1.grid(row=1, column=0)

        self.LoginFrame2 = LabelFrame(self.frame, width=1000, height=600,font=('arial',20,'bold'),relief='ridge')
        self.LoginFrame2.grid(row=2, column=0)

        self.lblUsername=Label(self.LoginFrame1, text = 'Username',font=('arial',20,'bold'),bd=22)
        self.lblUsername.grid(row=0,column=0)
        self.txtUsername=Entry(self.LoginFrame1,font=('arial',20,'bold'))
        self.txtUsername.grid(row=0,column=1)

        self.lblPassword=Label(self.LoginFrame1, text = 'Password',font=('arial',20,'bold'),bd=22)
        self.lblPassword.grid(row=1,column=0)
        self.txtPassword=Entry(self.LoginFrame1,font=('arial',20,'bold'),show="*")
        self.txtPassword.grid(row=1,column=1)


        self.btnLogin = Button(self.LoginFrame2, text = 'Login', width = 17,font=('arial',20,'bold'),command =self._login_btn_clicked)
        self.btnLogin.grid(row=3,column=0, pady=20, padx=8)

        self.btnExit = Button(self.LoginFrame2, text = 'Exit', width = 17,font=('arial',20,'bold'),command =self.iExit)
        self.btnExit.grid(row=3,column=2, pady=20, padx=8)
        #==============================Buttons===========================================================

    def verifycred(self,username,password):
        if username == "hello" and password == "password":
            return 1
        else:
            return 0

    def _login_btn_clicked(self):
        username = self.txtUsername.get()
        password = self.txtPassword.get()
        verify_out = self.verifycred(username,password)
        if verify_out == 1:
            self.newWindow = Toplevel(self.master)
            self.app = Window2(self.newWindow)
            self.master.destroy()

        else:
            tkinter.messagebox.showerror("Login error", "Incorrect Credentials")

    def iExit(self):
        self.iExit = tkinter.messagebox.askyesno("Login Systems", "Confirm you want to exit")
        if self.iExit > 0:
            self.master.destroy()
        else:
            command = self.new_window
            return

    def new_window(self):
        self.newWindow = Toplevel(self.master)
        self.app = Window2(self.newWindow)

class Window2(Window1):
    def __init__(self, master):
        self.master =master
        self.master.title("Welcome to the System")
        self.frame = Frame(self.master)
        self.frame.pack()
        root = Tk()
        root.title("Options")
        Tops = Frame(root,bd=20,pady=5)
        Tops.pack(side=TOP)

        self.frame = Frame(self.master)
        self.frame.pack()
        lblTitle = Label(Tops,font=('arial',30,'bold'),text='Welcome')
        lblTitle.grid(row=0,column=0)

        buttonPresident = Button(Tops,text='to Window 3', width = 20,font=('arial',20,'bold'),command=self.testing)
        buttonPresident.grid(row=1,column=0, pady=20, padx=8)

    def testing(self):
        # root.withdraw()
        main = Tk()
        self.newWindow = Toplevel(main)
        self.app = Window3(self.newWindow)


class Window3(Window1):
    def __init__(self, master):
        self.master =master
        self.master.title("Window 3")

        myLabel1 = Label(self.master, text="Hello World").grid(row=0,column=0)


if __name__== '__main__':
    root = Tk()

    application = Window1(root)
    root.mainloop()

0 个答案:

没有答案