我一直在调试一个程序,该程序将从用户指定的目录中获取文件,对其进行转换,然后在另一个用户指定的目录中创建新文件。我现在收到一个似乎无法弄清的错误。 start_command
函数从directory
输入框中获取e1
。我的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()
答案 0 :(得分:0)
使用此代码:
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)
需要进一步的调整,没有Progressbar
和convertor
之类的东西就无法进一步发展。
我所做的是我通过start_command
,browsepath
和browsespd
的本地方法的不识别性来识别它们,这意味着您不需要传递它们的PageOne
参数,但是现在您必须使用self
或self.start_command
之类的名称来调用它们。
希望有帮助!