我正试图通过菜单栏打开一个文件。但是,它总是给我这个错误“TypeError:unbound method openfile()must be called with hash instance as first argument(got nothing instead)”我以前从未遇到过这个错误,所以我不知道该怎么办。我正在使用Tkinter作为我的GUI。你知道吗
class application:
def openfile():
filename = askopenfilename(parent=root)
f = open(filename)
f.read()
print (filename)
def hashmd5():
BLOCKSIZE = 65536
hasher = hashlib.md5()
with open(askopenfilename(), 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
print(hasher.hexdigest())
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Save", command=filemenu)
filemenu.add_command(label="Open", command=hash.openfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
更新: 我设法解决了我的代码错误,它的工作很好,但当我试图添加一个登录窗口使用标签和按钮,窗口没有出现在所有。你知道吗
import hashlib
import Tkinter as tk
from tkFileDialog import askopenfilename
class Application(object):
def __init__(self):
self.root = root = tk.Tk()
menubar = tk.Menu(root)
root.title("Hashing Tool")
root.geometry("300x150")
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)
self.filename = tk.StringVar()
self.filename.set("No File Selected")
lbl = tk.Label(root, textvariable=self.filename, anchor="w")
lbl.pack()
self.digest = tk.StringVar()
lbl = tk.Label(root, textvariable=self.digest, anchor="w")
lbl.pack()
def create_widgets(self):
self.instruction = Label(self ,text ="enter")
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")
self.submit_button.grid(row = 2, column = 0, sticky = W)
self.text = Text(self,width = 35, height = 5, wrap = WORD)
def reveal(self):
cotent = self.password.get()
if content == "password":
message = "Accessed"
else:message = "Denied"
self.text.insert(0,0,message)
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()
有多种方法可以组织代码。有一种方法:
如果您在python3上运行这个命令,您需要更改Tkinter import语句。你知道吗
类方法要求它们的第一个参数是
self
,并且必须在类的实例上调用。你知道吗如果您试图创建静态方法(在类本身上调用),那么应该在方法定义之前加上
@staticmethod
。你知道吗例如
相关问题 更多 >
编程相关推荐