Tkinter - 打开一个窗口并关闭另一个窗口

时间:2016-04-10 12:27:24

标签: python tkinter

我想要一个登录屏幕,当登录成功时,屏幕关闭并创建一个新屏幕。问题是,当我喜欢以下代码时,两个屏幕同时打开。如果您有任何改进代码的建议,请做! :)

from Tkinter import *
import mysql.connector
import tkMessageBox

class Tela_login(Frame):
    root = Tk()
    root.geometry("1024x768")
    root.resizable(width=FALSE, height=FALSE)
    background_image = PhotoImage(file="fundo.gif")
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        fundo = Label(image=self.background_image)
        fundo.place(x=0,y=0,relwidth=1,relheight=1)
        self.create_screen()

    def create_screen(self):
        self.label1 = Label(text="Login",font = ("Arial",60))
        self.label2 = Label(text="Senha",font = ("Arial",60))
        self.login = Entry(font = ("Arial",60),width = 10)
        self.senha = Entry(show="*",font = ("Arial",60), width= 10)
        self.entrar = Button(text="Entrar",command=lambda : self.efetua_login(),font = ("Arial",60),width=10)

        self.label1.grid(padx=258,pady=(70,0))
        self.login.grid(padx=258)
        self.label2.grid(padx=258,pady=(50,0))
        self.senha.grid(padx=258)
        self.entrar.grid(padx=258,pady=(50,0))

    def efetua_login(self):
        login = self.login.get()
        senha = self.senha.get()

        cnx = mysql.connector.connect(user='root', password='123qwe', host='192.168.56.1', database='teste')
        cursor = cnx
        cursor = cnx.cursor()

        query = ("SELECT nome, senha FROM funcionario WHERE nome = %s AND senha = %s")
        cursor.execute(query, (login,senha))
        row = cursor.fetchone()

        if row is None:

            tkMessageBox.showinfo("Erro","Usuario ou Senha Incorretos")

        else:
            app2 = Tela_principal()
            self.root.destroy()
            Tela_principal.root.mainloop()


class Tela_principal(Frame):
    root = Tk()
    root.geometry = ("1024x768")
    root.resizable(width=FALSE, height=FALSE)

    def inicia(self, master):
        background_image = PhotoImage(file="fundo.gif")
        app2 = Tela_principal(self.root)
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

app = Tela_login(Tela_login.root)
Tela_login.root.mainloop()

1 个答案:

答案 0 :(得分:0)

杀手,我确实喜欢你说的,它就像一个魅力! 以下是示例代码:

from tkinter import *

class Tela_login(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.grid()
        self.button1 = Button(text = "Open",command = lambda: self.open_login())
        self.button1.grid()

    def open_login(self):
        root2 = Toplevel()
        app2 = Tela_principal(root2)

class Tela_principal(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.grid

root = Tk()
root.geometry("800x600")
app = Tela_login(root)
root.mainloop()