Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类“Call”实例作为第一个参数错误“Tkinter”_Python_Class_Tkinter - Fatal编程技术网

Python 类“Call”实例作为第一个参数错误“Tkinter”

Python 类“Call”实例作为第一个参数错误“Tkinter”,python,class,tkinter,Python,Class,Tkinter,我正在制作一个Tkinter文本编辑器,我在课堂上遇到了一些问题。我创建了一个包含所有函数的类,以及一个包含mainloop的类。根窗口打开的很好,但是当按下保存按钮时,我得到了错误 Exception in Tkinter callback Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter

我正在制作一个Tkinter文本编辑器,我在课堂上遇到了一些问题。我创建了一个包含所有函数的类,以及一个包含mainloop的类。根窗口打开的很好,但是当按下保存按钮时,我得到了错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
TypeError: unbound method saveas() must be called with F instance as first argument (got nothing instead)
我已经看过这个和这个例子,但无法找出哪里出了问题。 我有什么问题

下面是我的简短代码,说明了这个问题

from Tkinter import *
class Main(object):
    def __init__(self, root):
        root.title("PyText")
        self.m1=Menu(root)
        self.fm=Menu(self.m1, tearoff=0)
        self.fm.add_command(label="New", command=self.saveas())
        self.fm.add_command(label="Open", command=self.saveas())
        self.fm.add_command(label="Save", command=self.saveas())
        self.fm.add_command(label="Save as...", command=self.saveas())
        self.fm.add_command(label="Close", command=self.saveas())
        self.fm.add_separator()

        self.fm.add_command(label="Exit", command=root.quit)
        self.m1.add_cascade(label="File", menu=self.fm)
        self.editmenu = Menu(self.m1, tearoff=0)
        self.editmenu.add_command(label="Undo", command=self.saveas())

        self.editmenu.add_separator()

        self.editmenu.add_command(label="Cut", command=self.saveas())
        self.editmenu.add_command(label="Copy", command=self.saveas())
        self.editmenu.add_command(label="Paste", command=self.saveas())
        self.editmenu.add_command(label="Delete", command=self.saveas())
        self.editmenu.add_command(label="Select All", command=self.saveas())

        self.m1.add_cascade(label="Edit", menu=self.editmenu)
        self.helpmenu = Menu(self.m1, tearoff=0)
        self.helpmenu.add_command(label="Help Index", command=self.saveas())
        self.helpmenu.add_command(label="About...", command=self.saveas())
        self.m1.add_cascade(label="Help", menu=self.helpmenu)

        root.config(menu=self.m1)


        self.t1=Text(root)
        self.t1.config(width=90, height=40)
        self.t1.grid()
    def saveas(self):
        self.filewin = Toplevel()
        self.e1=Entry(self.filewin)
        self.e1.grid()
        self.button = Button(self.filewin, text="Save", command=self.save)
        self.button.grid()

    def save(self):
        with open(self.e1.get(), "w") as f: # this instance variable can be accessed
            f.write(self.t1.get('1.0', 'end')) # added self.t1 above, and start/end


root = Tk()
app = Main(root)
root.mainloop()

您构建应用程序时出错。从该类中取出根Tkinter对象构建代码。您需要一个包含Tkinter对象内容的类。如果您想为其他类型的对象创建更多的类,那很好。实际上,您正在调用
F.saveas
作为一个没有参数的类方法,并且它需要
self
,它是类
F
的一个实例,您没有它,因为您从未实例化过它

from Tkinter import *
class Main(object):
    def __init__(self, root):
        # root.geometry("100x100") # this will make your app window tiny -
        # I've commented it out for now but you'll need to look into it
        self.b1 = Button(root, text="save", command=self.saveas)
        self.b1.grid()
        self.t1 = Text(root) # added a Text widget
        self.t1.grid()
    def saveas(self):
        filewin = Toplevel()
        self.e1=Entry(filewin) # make this an instance variable
        self.e1.grid()
        button = Button(filewin, text="Save", command=self.save)
        button.grid()
    def save(self):
        with open(self.e1.get(), "w") as f: # this instance variable can be accessed
            f.write(self.t1.get('1.0', 'end')) # added self.t1 above, and start/end

master = Tk()
app = Main(master)
root.mainloop()

我现在编辑了我的代码以包含我的完整代码,因为这样做会产生更多的错误。@JonahFleming-这样做会产生什么错误?当我将所有的
command=self.saveas()
更改为
command=self.saveas
时,它似乎工作得很好。我会尝试一下,但我让所有的菜单项在程序开始时运行它们的命令。@JonahFleming-这就是我更改所有
command=self.saveas()的原因
to
command=self.saveas
正如我在上一篇评论中所说。谢谢你,这真的很有用+1