无法调用openfile函数

时间:2015-12-03 14:46:33

标签: python tkinter

我试图通过菜单栏打开文件。但是,它一直给我这个错误" TypeError:unbound方法openfile()必须使用哈希实例作为第一个参数调用(没有任何代替)"我之前从未遇到过这个错误,所以我不知道该怎么做。我使用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()

2 个答案:

答案 0 :(得分:1)

类方法要求它们的第一个参数为self,并且必须在类的实例上调用。

如果您正在尝试制作静态方法(要在类本身上调用),则应在方法定义之前加@staticmethod

e.g。

class hash:
    @staticmethod
    def openfile():
        # ...

答案 1 :(得分:0)

有各种方法来组织代码。这是一种方式:

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)

        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()

如果在Python 3上运行此命令,则需要更改Tkinter导入语句。