【发布时间】: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