NameError:尝试单击菜单栏时未定义名称“root”

时间:2018-06-08 04:19:16

标签: python tkinter nameerror

我会尝试具体,当我试图点击'新'选项菜单栏时,它会给我一个错误,说明它没有定义根,任何帮助为什么会发生这种情况?:

  

file.py“,第6行,在newfile中       root.filename = filedialog.askopenfilename(initialdir =“/ Libraries / Document”,title =“Select file”,filetypes =((“lre   files“,” .lre“),(”所有文件“,”。*“)))NameError:名称”root“不是   定义

我有两个文件,我有这个代码file.py:

from tkinter import filedialog
from tkinter import *

def newfile():
    print('hello')
    root.filename =  filedialog.askopenfilename(initialdir = "/Libraries/Document",title = "Select file",filetypes = (("lre files","*.lre"),("all files","*.*")))
    print (root.filename)

另一部分是这个菜单:

from tkinter import filedialog
from tkinter import *
import library.file as New

def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

root = Tk()
root.state("zoomed")
root.title("Interactive Toys")
menubar = Menu(root)

filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=New.newfile)
menubar.add_cascade(label="File", menu=filemenu)

root.config(menu = menubar)
root.mainloop()

0 个答案:

没有答案