1
我實現了我的哈希工具的登錄窗口,但問題是我希望它被編程,當我點擊提交按鈕時,它會自動將我重定向到哈希工具窗口。現在,我必須關閉登錄頁面才能重定向到其他窗口。登錄後不能打開新窗口
import hashlib
import Tkinter as tk
from tkFileDialog import askopenfilename
import sys
from Tkinter import *
class Start(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction = Label(self ,text ="Enter password")
self.instruction.grid(row = 0,column = 0, columnspan = 2,sticky =W)
self.password = Entry(self)
self.password.grid(row = 1, column = 1, sticky = W)
self.submit_button = Button(self, text = "Submit", command = self.reveal)
self.submit_button.grid(row = 2, column = 0, sticky =W)
self.text = Text(self, width = 35, height = 5, wrap = WORD)
self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def reveal(self):
content = self.password.get()
if content == "password":
message = "you have access to something special"
else:message = "denied"
self.text.insert(0.0,message)
root = Tk()
root.title("Password")
root.geometry("250x150")
app = Start(root)
root.mainloop()
class Application(object):
def __init__(self):
self.root = root = tk.Tk()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Hash", command=self.hashmd5)
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
#Add a Label to hold the current filename
self.filename = tk.StringVar()
self.filename.set("No File Selected")
lbl = tk.Label(root, textvariable=self.filename, anchor="w")
lbl.pack()
#Add a Label to hold the most recent MD5 digest
self.digest = tk.StringVar()
lbl = tk.Label(root, textvariable=self.digest, anchor="w")
lbl.pack()
root.mainloop()
def hashmd5(self):
BLOCKSIZE = 65536
hasher = hashlib.md5()
filename = askopenfilename(parent=self.root)
self.filename.set(filename)
print(filename)
with open(filename, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
digest = hasher.hexdigest()
self.digest.set(digest)
print(digest)
Application()
謝謝!我發現了一個小錯誤,用戶如果關閉登錄窗口,仍然可以訪問哈希窗口。有什麼辦法可以阻止它嗎? – JoanT
@JoanT,我更新後回答。 – falsetru
可能值得注意的是,銷燬根窗口然後在同一程序中創建一個新窗口是有點不尋常的。這通常不是GUI如何設計的。 –