在Tkinter上更改窗口

时间:2015-06-24 15:26:12

标签: python python-2.7 tkinter

如何在Tkinter上更改窗口?我想将Login的窗口更改为ListaBase的窗口,但我不知道如何制作此窗口。

# -*- coding: latin-1 -*-
from Tkinter import *

import tkMessageBox

import sys
sys.path.insert(0,'C:\\Users\\felipe.cunha\\Documents\\project')

from conexao.conexao import Conexao


class Login(Frame):
        def __init__(self):
                Frame.__init__(self)
                #self.master = master
                self.master.title("teste")

                self.host = "localhost"
                label_menu = Label(self.master,text = 'Login')
                label_menu.pack()

                self.entrada_menu_login = Entry(self.master)# user
                self.entrada_menu_senha = Entry(self.master, show = "*")#passwd 
                self.entrada_menu_login.focus()               
                self.entrada_menu_login.pack()
                self.entrada_menu_senha.pack()

                btn_login = Button(self.master,text="Logar",command = self.btnConnection)
                btn_login.pack()


        def show_entry_fields(self):
                print(type(self.entrada_menu_login.get()), type(self.entrada_menu_senha.get()))

        def btnConnection(self):
                conn = Conexao(self.host,self.entrada_menu_login.get(),self.entrada_menu_senha.get())#,"dsti")
                self.dbCursor = conn.getConnection()


class ListaBases(Frame):
        def __init__(self,master,cursor):
                self.master = master
                #self.master.geometry("400x400")
                self.master.title("Data Bases")

                menubar = MenuBar(self.master)

                label_database = Label(self.master,text = "Lista de Banco de Dados").grid(row=1,column=0)
                #ListBox
                listBox = Listbox(self.master)
                listBox.grid(row=2,column=0)
                cursor.execute("show databases")


                for base in cursor.fetchall():
                        print base
                        listBox.insert(END,base)
                listBox.bind('<<ListboxSelect>>',self.selectBase)

                cursor.close()

        def selectBase(self,e):
                w = e.widget
                index = int(w.curselection()[0])
                self.value = w.get(index)
                print self.value[0]
                ListaTables(self.master,self.value[0])

class MenuBar():
        def __init__(self,master):
                self.master = master
                self.master.title("Menu Principal")
                menu_bar = Menu(self.master)
                self.master.config(menu = menu_bar)

                file_menu = Menu(menu_bar)
                file_menu.add_command(label= 'Open')
                menu_bar.add_cascade(label="File",menu=file_menu)
                menu_bar.add_cascade(label="Shell")


class ListaTables(Frame):
        def __init__(self,master,base):
                self.master = master
                self.master.title("Tables")
                cursor = Conexao("localhost","root","d04m10").getConnection()       

                cursor.execute("use %s"%(base))

                label_database = Label(self.master,text = "Lista de Tabelas").grid(row=1,column=0)
                #ListBox
                listBox = Listbox(self.master)
                listBox.grid(row=2,column=0)
                cursor.execute("show tables")


                for table in cursor.fetchall():
                        print table
                        listBox.insert(END,table[0])

1 个答案:

答案 0 :(得分:0)

我建议将“base”作为根窗口,将登录窗口作为Toplevel的实例。您可以最初隐藏基本窗口,或者只是禁用它并弹出登录窗口。

另一种解决方案是使您的基本窗口成为一个框架,并将您的登录窗口作为一个框架。首先,您将打包/放置/网格登录窗口。当用户登录时,删除登录框并打包/放置/网格基础框架。