我正在练习一个简单的登录系统。我有一个主程序窗口,我想在登录窗口出现时隐藏它。输入正确的密码后,我想要破坏登录窗口并重新出现主窗口(取消图标)。
主程序代码(LibrarySQL.py):
from tkinter import *
import libraryentrySQL, librarydatabase, login
import sqlite3
import os
def click():
entered_text = entry.get() #collect text from text entry box
output.delete(0.0,END) #clears text box - start clearing from 0.0 (from line 0) to END (after last character)
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute("SELECT definition FROM Dictionary WHERE word = ?", (entered_text,))
for row in c:
definition = row[0]
break
else:
definition = "No word found in dictionary, try again!"
output.insert(END, definition) #this inserts the contents of variable 'definition' at the beginning (END) - because it was cleared before, END is the at the start
def clickentry(): #this function is run when the 2nd button (entry is pressed)
def definition_submitted(word, definition):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute("INSERT INTO Dictionary VALUES (?, ?)", (word, definition))
new_db.commit()
new_db.close()
definition_window = libraryentrySQL.DefinitionWindow(window, definition_submitted) #this creates the object 'definition window' and passes to it 'the window variable'
#so that it can have a canvas
def clickdataview():
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute("SELECT * FROM Dictionary")
data = c.fetchall()
count = len(data)
database_window = librarydatabase.DatabaseWindow(window, count, data)
new_db.close()
def login_window(window):
log = login.Login(window)
window = Tk()
login_window(window)
window.withdraw()
window.deiconify()
window.title("My Little Dictionary")
#Create the Label
Label(window, text="Enter the word you want defining:").grid(row=0, column=0, sticky=W)
#create entry box
entry=Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
#create submit button
Button(window, text="Submit", width=5, command=click).grid(row=2, column=0, sticky=W)
#create second label
Label(window, text="\nDefinition").grid(row=3, column=0, sticky=W)
#create text box
output=Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, sticky=W)
#create submit button to open enter new definition window
Button(window, text="Enter a New Definition", width=20, command=clickentry).grid(row=5, column=0, sticky=W)
#create open database button to open database view window
Button(window, text="View Dictionary", width=20, command=clickdataview).grid(row=6, column=0, sticky=W)
#Create the Dictionary.db if not already present
if not os.path.isfile("dictionary.db"):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute('''CREATE TABLE Dictionary
(word text,
definition text)''')
c.execute('''INSERT INTO Dictionary VALUES
('Algorithm', 'Step by step instructions to complete a task')''')
new_db.commit()
new_db.close()
window.mainloop()
登录类代码(login.py):
from tkinter import *
class Login(Toplevel):
def __init__(self, window):
Toplevel.__init__(self, window)
self.title("Current Library")
Label(self, text="Log in to use this program:").grid(row=0, column=0, sticky=W)
self.userbox=Entry(self, width=20, bg="light green")
self.userbox.grid(row=1, column=0, sticky=W)
self.passbox=Entry(self, width=20, bg="light green")
self.passbox.grid(row=2, column=0, sticky=W)
Button(self, text="Submit", width=5, command=self.clicked).grid(row=3, column=0, sticky=W)
def clicked(self):
username = self.userbox.get()
password = self.passbox.get()
if password == "password":
self.correct = True
self.destroy()
else:
pass
我确切地知道问题所在:由于主循环中的指令顺序,主窗口隐藏然后立即重新出现。 因此,如何正确输入密码,我可以如何隐藏主要内容并显示登录窗口?之后我希望登录窗口销毁,主要重新出现?
感谢任何帮助。
答案 0 :(得分:1)
您可以修改Toplevel实例的clicked()
方法来调用deiconify
,这样只有在Toplevel被销毁时才会调用它。
class Login(Toplevel):
def __init__(self, window):
Toplevel.__init__(self, window)
# this is the parent/root window
self.window = window
Button(self, text='show main', command=self.click).pack()
def click(self):
self.destroy() # destroy the toplevel
self.window.deiconify() # and restore the root window
在你的主要模块中:
root = Tk()
# make the login instance
Login(root)
# withdraw the main window
root.withdraw()
# set up widgets in main window
...
mainloop()