![](/img/trans.png)
[英]How can I make a tkinter Toplevel window dependent on another Toplevel window
[英]How can I hide the main tkinter program window whilst logging in using another Toplevel window?
我正在使用一個簡單的登錄系統進行練習。 我有一個希望隱藏的主程序窗口,同時出現登錄窗口。 輸入正確的密碼后,我希望破壞登錄窗口,並重新顯示主窗口(取消圖標化)。
主程序代碼(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
我確切地知道問題出在哪里:由於主循環中的指令順序,主窗口隱藏起來,然后立即重新出現。 因此,在正確輸入密碼之前,如何隱藏主窗口並顯示登錄窗口? 在此之后,我希望破壞登錄窗口並重新顯示主窗口嗎?
任何幫助將不勝感激。
您可以修改deiconify
實例的clicked()
方法以調用deiconify
,這樣僅在頂級被銷毀時才調用它。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.