TypeError:openFile()恰好接受1个参数(给定0)

时间:2019-11-11 09:56:35

标签: python self

出现错误“ TypeError:openFile()恰好接受1个参数(给定0) “

如果我从打开的File方法中删除了self,它将运行该方法并打开文件选择器窗口,但随后我将无法访问其他的refreshVECs方法

from Tkinter import Tk, Menu, Listbox
from tkFileDialog import askopenfilename
from xml.dom import minidom


class main:
    global filename
    filename = ""
    global mydoc
    mydoc = ""

    def refreshVECs(self,mydoc):
        VECS = self.getVECs(mydoc)
        counter = 0
        self.vecBox.delete(0, 'end')
        for elem in VECS:
            vecString = elem.attributes['id'].value
            self.vecBox.insert(counter, vecString)
            counter = counter + 1

    def openFile(self):
        Tk().withdraw()
        self.filename = askopenfilename()
        print('\n FileName ', filename)
        self.mydoc = minidom.parse(filename)
        self.refreshVECs(mydoc)

    root = Tk()
    root.title("File Viewer V1.0")
    root.geometry("600x600")


    # ===========MENU======
    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="New", command="")
    filemenu.add_command(label="Open", command=openFile)
    filemenu.add_command(label="Save", command='')
    menubar.add_cascade(label="File", menu=filemenu)
    root.config(menu=menubar)


    vecBox = Listbox(root, width=50, height=14)
    vecBox.grid(row=1, column=0, padx=15, pady=5,columnspan = 3)
    root.mainloop()

mymain = main()

1 个答案:

答案 0 :(得分:-1)

类主要:     全局文件名     filename =“”     全局mydoc     mydoc =“”

def refreshVECs(self,mydoc):
    VECS = mydoc.getElementsByTagName('VEC')
    counter = 0
    self.vecBox.delete(0, 'end')
    for elem in VECS:
        vecString = elem.attributes['id'].value
        self.vecBox.insert(counter, vecString)
        counter = counter + 1

def openFile(self):
    Tk().withdraw()
    filename = askopenfilename()
    print('\n FileName ', filename)
    mydoc = minidom.parse(filename)
    self.refreshVECs(mydoc)

# ===========MENU======
def __init__(self, master):
    self.master = master
    master.title("A simple GUI")
    master.geometry("600x600")
    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="New", command="")
    filemenu.add_command(label="Open", command=self.openFile)
    filemenu.add_command(label="Save", command='')
    menubar.add_cascade(label="File", menu=filemenu)
    root.config(menu=menubar)

    self.vecBox = Listbox(root, width=50, height=14)
    self.vecBox.grid(row=1, column=0, padx=15, pady=5,columnspan = 3)
    root.mainloop()

root = Tk() mymain = main(root)

相关问题