【问题标题】:start_command() missing 1 positional argument?start_command() 缺少 1 个位置参数?
【发布时间】:2020-05-07 13:54:17
【问题描述】:

我一直在调试一个程序,该程序从用户指定的目录获取文件,转换它们,并在另一个用户指定的目录中创建一个新文件。我现在收到一个我似乎无法弄清楚的错误。 start_command 函数从e1 输入框中获取directory。我的browse 函数可以完美地获取目录路径并将其放在e1 中。我使用self.directory.get() 获取路径并将其发送到start_command() 中的不同功能。我看不到我在这里遗漏了什么。任何见解都会有所帮助。

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "/root/Desktop/I.T.E.D./ITED.py", line 108, in <lambda>
    b1 = Button(self, text='Start', width=12, command=lambda: start_command(self.excel_file.get(), self.directory.get()))
TypeError: start_command() missing 1 required positional argument: 'directory'

代码:

from tkinter import *
import tkinter as tk
import hashlib
import converter
import time
from tkinter.ttk import *
from tkinter import filedialog


class ITED(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F,geometry in zip((StartPage, PageOne), ('250x100', '550x250')):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = (frame, geometry)
            frame.grid(row=0, column=0, sticky="nsew")

        print(self.frames)

        self.show_frame("StartPage")


    def show_frame(self, page_name):
        frame, geometry = self.frames[page_name]
        self.update_idletasks()
        self.geometry(geometry)
        frame.tkraise()


    def quit(self):
        self.app.destroy()



class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self,parent)

        l1 = Label(self, text='Please enter key:')
        l1.pack(side=TOP, expand=True)

        self.password = StringVar()
        e3 = Entry(self, textvariable=self.password)
        e3.pack(side=TOP, expand=True)

        b6 = Button(self, text='Validate', width=12, command=lambda: self.admin(self.password.get()))
        b6.pack(side=LEFT, expand=True)
        b7 = Button(self, text='Close', width=12, command=quit)
        b7.pack(side=LEFT, expand=True)


    def admin(self, password):
        admin_key = '72b302bf297a228a75730123efef7c41'
        hash_object = hashlib.md5(password.encode())
        key = hash_object.hexdigest()

        if key == admin_key:
            print('Success...')
            self.destroy()
            self.controller.show_frame("PageOne")

        else:
            print('Permission Denied...')
            print(key)


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)

        l2 = Label(text='Use the browse buttons to select the proper folder.')
        l2.pack(side=TOP)
        l3 = Label(text='PDF Directory: The location of the folder containing the PDFs you wish to convert')
        l3.pack(side=TOP)
        l4 = Label(text='Excel Directory: The location you wish to create the Excel file within')
        l4.pack(side=TOP)
        instructions1 = Label(self, text='PDF Directory: ')
        instructions1.pack()
        self.directory = StringVar()
        e1 = Entry(self, textvariable=self.directory)
        e1.pack()
        b3 = Button(self, text='Browse', width=8, command=lambda: browsepath())
        b3.pack()
        instructions2 = Label(self, text='Excel Directory: ')
        instructions2.pack()
        self.excel_file = StringVar()
        e2 = Entry(self, textvariable=self.excel_file)
        e2.pack()
        b4 = Button(self, text='Browse', width=8, command=lambda: browsespd())
        b4.pack()

        progress = Progressbar(self, orient=HORIZONTAL, length=500, mode='determinate')
        progress.pack(side=BOTTOM)

        b1 = Button(self, text='Start', width=12, command=lambda: self.start_command(self.excel_file.get(), self.directory.get()))
        b1.pack(side=LEFT)
        b2 = Button(self, text='Close', width=12, command=quit)
        b2.pack(side=RIGHT)




        def start_command(self, excel_file, directory):
            converter.create_json(directory)
            converter.create_xlsx(excel_file, directory)
            converter.check_if_existing(directory)
            converter.convert(new_list, directory)


        def browsepath():
            foldername = filedialog.askdirectory()
            e1.delete(0, END)
            e1.insert(END, foldername)


        def browsespd():
            filename = filedialog.askdirectory()
            e2.delete(0, END)
            e2.insert(END, filename)


        def bar():
            progress['value'] = 20
            self.update_idletasks()
            time.sleep(1)

            progress['value'] = 40
            self.update_idletasks()
            time.sleep(1)

            progress['value'] = 50
            self.update_idletasks()
            time.sleep(1)

            progress['value'] = 60
            self.update_idletasks()
            time.sleep(1)

            progress['value'] = 80
            self.update_idletasks()
            time.sleep(1)
            progress['value'] = 100


if __name__ == "__main__":
    app = ITED()
    app.title("I.T.E.D. Converter")
    app.mainloop()

【问题讨论】:

  • 不尝试代码,我猜start_command 需要一个self 参数作为第一个参数,在你的情况下是self.excel_file.get() 或使用self.start_command

标签: python oop tkinter error-handling typeerror


【解决方案1】:

使用此代码:

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)

        l2 = Label(text='Use the browse buttons to select the proper folder.')
        l2.pack(side=TOP)
        l3 = Label(text='PDF Directory: The location of the folder containing the PDFs you wish to convert')
        l3.pack(side=TOP)
        l4 = Label(text='Excel Directory: The location you wish to create the Excel file within')
        l4.pack(side=TOP)
        instructions1 = Label(self, text='PDF Directory: ')
        instructions1.pack()
        self.directory = StringVar()
        e1 = Entry(self, textvariable=self.directory)
        e1.pack()
        b3 = Button(self, text='Browse', width=8, command=lambda: browsepath())
        b3.pack()
        instructions2 = Label(self, text='Excel Directory: ')
        instructions2.pack()
        self.excel_file = StringVar()
        e2 = Entry(self, textvariable=self.excel_file)
        e2.pack()
        b4 = Button(self, text='Browse', width=8, command=lambda: browsespd())
        b4.pack()

        progress = Progressbar(self, orient=HORIZONTAL, length=500, mode='determinate')
        progress.pack(side=BOTTOM)

        b1 = Button(self, text='Start', width=12, command=lambda: self.start_command(self.excel_file.get(), self.directory.get()))
        b1.pack(side=LEFT)
        b2 = Button(self, text='Close', width=12, command=quit)
        b2.pack(side=RIGHT)




    def start_command(self, excel_file, directory):
        converter.create_json(directory)
        converter.create_xlsx(excel_file, directory)
        converter.check_if_existing(directory)
        converter.convert(new_list, directory)


    def browsepath(self):
        foldername = filedialog.askdirectory()
        e1.delete(0, END)
        e1.insert(END, foldername)


    def browsespd(self):
        filename = filedialog.askdirectory()
        e2.delete(0, END)
        e2.insert(END, filename)

如果没有Progressbarconvertor 之类的东西,经过进一步的调整,就无法进一步发展。

我所做的是我通过不识别它们来制作start_commandbrowsepathbrowsespdPageOne 本地方法,这意味着您不需要传递它们self 参数,但是您现在必须使用self.start_commandpageOne_object.start_command 之类的方式调用它们。

希望对你有帮助!

【讨论】:

  • 谢谢。我是 OOP 的新手,我正在努力学习,所以每一个小问题都会教会我一些东西。但是,在从start_command(self.excel_file.get(), self.directory.get())) 调整到self.start_command(self.excel_file.get(), self.directory.get())) 之后,我现在得到一个 AttributeError:'PageOne' 对象没有属性'start_command'。我正在更新代码部分以包含整个前端代码。一切正常,直到我开始扩展项目以接受用户指定的路径和多个 tkinter 窗口。
  • 取消缩进从 def start_commande2.insert... 的所有内容
  • 那么,我不确定这是怎么发生的,但我没有注意到一切都被移动了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2021-09-15
  • 1970-01-01
  • 2019-10-10
  • 2017-03-27
  • 2019-11-12
相关资源
最近更新 更多