如何解决此错误?
try:
from Tkinter import *
except ImportError:
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from idlelib.tree import ScrolledCanvas, FileTreeItem, TreeNode
import os
import tkinter.messagebox
class FileManager(Tk):
def createWidgets(self):
self.menu = Menu(self)
self.submenu = Menu(self.menu)
self.menu.add_cascade(label='File', menu = self.submenu)
self.submenu.add_command(label='New Project')
self.submenu.add_command(label='Open...')
self.submenu.add_separator()
self.submenu.add_command(label='Save')
self.submenu.add_command(label='Save As...')
self.submenu.add_separator()
self.submenu.add_command(label='Exit')
self.editmenu = Menu(self.menu)
self.menu.add_cascade(label='Edit', menu = self.editmenu)
self.editmenu.add_command(label='Undo')
self.editmenu.add_command(label='Redo')
self.editmenu.add_separator()
self.editmenu.add_command(label='Cut')
self.editmenu.add_command(label='Copy')
self.editmenu.add_command(label='Copy Path')
self.editmenu.add_command(label='Paste')
self.editmenu.add_command(label='Select All')
self.editmenu.add_separator()
self.editmenu.add_command(label='Replace')
self.editmenu.add_command(label='Rename')
self.editmenu.add_command(label='Move to...')
self.optionsmenu = Menu(self.menu)
self.menu.add_cascade(label='Options', menu = self.optionsmenu)
self.optionsmenu.add_command(label='Very Large Icons')
self.optionsmenu.add_command(label='Large Icons')
self.optionsmenu.add_command(label='Medium Icons')
self.optionsmenu.add_command(label='Small Icons')
self.optionsmenu.add_command(label='List')
self.optionsmenu.add_command(label='Tiles')
self.optionsmenu.add_command(label='Details')
self.optionsmenu.add_command(label='Content')
self.optionsmenu.add_command(label='List')
self.optionsmenu.add_separator()
self.optionsmenu.add_command(label='Show Libraries')
self.optionsmenu.add_command(label='Show All Folders')
self.optionsmenu.add_command(label='')
self.optionsmenu.add_command(label='')
self.optionsmenu.add_command(label='')
def __init__(self,master=None):
Tk.__init__(self)
self.createWidgets()
self.config(menu=self.menu)
self.master.geometry("1000x500+200+100")
self.master.title("File Explorer")
self.pack(side="top")
self.entry=tkinter.ttk.Entry(master)
self.entry.pack(side="top",fill=X)
self.entry.focus()
self.sc = ScrolledCanvas(master, highlightthickness=0, takefocus=1)
self.sc.frame.pack(expand=0, fill="both", side="left")
item = FileTreeItem("C:")
node = TreeNode(self.sc.canvas, False, item)
node.expand()
self.fr=tkinter.ttk.Frame(master)
self.fr.pack(side="top",expand=True,fill="both")
self.contents=tkinter.ttk.Label(self.fr,text="Contents", style="BW.TLabel")
self.contents.pack(side="top",expand=False,fill=X)
self.entry.bind("<Return>",lambda e:self.buttonClick())
self.button=ttk.Button(self.entry,text="Find result",width=20)
self.button.pack(side="right")
self.button.config(command=self.buttonClick)
self.fr2=Text(self.fr)
self.scrollbar=tkinter.ttk.Scrollbar(self.fr2,command=self.fr2.yview)
self.scrollbar.pack(side="right",fill=Y)
self.fr2.configure(yscrollcommand=self.scrollbar.set)
self.fr2.pack(side="top",expand=True,fill="both")
self.fr3=tkinter.ttk.Frame(master)
self.fr3.pack(side="right",expand=True,fill="both")
self.open=ttk.Button(self.fr3,text='Open',command=self.openButton)
self.open.pack(side="left",fill=X,expand=True)
self.delete=ttk.Button(self.fr3,text="Delete",command=self.eraseButton)
self.delete.pack(side="left",fill=X,expand=True)
def buttonClick(self):
orisma=self.entry.get()
os.system(orisma)
self.fr2.delete('1.0','end')
try:
g=os.listdir(orisma)
times=len(g)
self.fr2.insert('end',orisma)
self.fr2.insert('end',' -->')
self.fr2.insert('end','\n')
self.fr2.insert('end','\n')
for i in range(times):
s=g[i]
self.fr2.insert('end',s)
self.fr2.insert('end','\n')
except:
tkinter.messagebox.showerror("Error", "Δεν εντοπίστηκε η διαδρομή!")
def openButton(self):
ranges = self.fr2.tag_ranges(SEL)
if ranges:
var=str(self.fr2.get(SEL_FIRST, SEL_LAST))
try:
end=var.index('\n')
if end>0:
print(var[:end])
except:
print(var)
else:
tkinter.messagebox.showerror("TclError ", "Δεν επιλέχθηκε αρχείο!")
def onClick(self):
pass
def __str__(self):
pass
def focus_get(self):
pass
def eraseButton(self):
pass
if __name__=="__main__":
root = FileManager()
root.mainloop()
我没有菜单栏。我曾经在课堂上使用Frame而不是Tk,但是菜单无法与Frame一起使用,因此我对其进行了更改,但仍然无法正常工作。有什么方法可以定义root = Tk()并以某种方式将其传递给类? 我是Python新手,不了解任何编程语言。另外,我的英语很烂,所以请表现出一些理解:P 无论如何,如果有人可能有空闲时间查看我的代码的一小部分,并帮助我找到解决此错误的方法,我将非常高兴:D