Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 3.x 从tkinter中的其他类调用按钮函数_Python 3.x_Oop_Tkinter - Fatal编程技术网

Python 3.x 从tkinter中的其他类调用按钮函数

Python 3.x 从tkinter中的其他类调用按钮函数,python-3.x,oop,tkinter,Python 3.x,Oop,Tkinter,我想创建一个使用其他类的函数(SaveFile)的按钮,但它抛出TypeError "TypeError: SaveFile() missing 1 required positional argument: 'self'" 我的代码是: import tkinter as tk class App1(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent)

我想创建一个使用其他类的函数(SaveFile)的按钮,但它抛出TypeError

"TypeError: SaveFile() missing 1 required positional argument: 'self'"
我的代码是:

import tkinter as tk

class App1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Button(self, text="Browse", command=self.SaveFile).grid(row=4, column=4)
        self.t1 = tk.Text(self, height=1, width=40, font="Times 9")
        self.t1.grid(row=2, column=3)

    def SaveFile(self):
        global name2
        name2 = asksaveasfilename(initialdir="\\", filetypes=(("Html Files", "*.html"),("All Files","*.*")),
                                  title = "Output path.")
        self.t1.delete("1.0", tk.END)
        self.t1.insert(tk.END, name2)

class App2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        self.b1 = tk.Button(self, text="Browse", command=App1.SaveFile)
        self.b1.grid(row=2, column=4)

好的,举一个更具体的例子:

import tkinter as tk

class App1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Button(self, text="Browse", command=self.SaveFile).grid(row=4, column=4)
        self.t1 = tk.Text(self, height=1, width=40, font="Times 9")
        self.t1.grid(row=2, column=3)

    def SaveFile(self):
        global name2
        name2 = asksaveasfilename(initialdir="\\", filetypes=(("Html Files", "*.html"),("All Files","*.*")),
                                  title = "Output path.")
        self.t1.delete("1.0", tk.END)
        self.t1.insert(tk.END, name2)

class App2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        self.b1 = tk.Button(self, text="Browse", command=App1.SaveFile)
        self.b1.grid(row=2, column=4)

app1_inst = App1(parent, controller) # this creates an instance
app2_inst = App2(parent, controller) # this also creates an instance

App1.SaveFile # this is being called a class method, it is attached to the class so self does not get passed
app1_inst.SaveFile # is being called as an instance method where self = app1_inst
由于app2_inst没有对app1_inst的任何引用,因此无法将其作为实例方法调用。调用App1.SaveFile不会传入实例,因为它甚至不知道是否存在实例

您需要更改App2的定义,以便能够传入(并可能保留对)App1的实例

e、 g。 将tkinter作为tk导入

class App1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        tk.Button(self, text="Browse", command=self.SaveFile).grid(row=4, column=4)
        self.t1 = tk.Text(self, height=1, width=40, font="Times 9")
        self.t1.grid(row=2, column=3)

    def SaveFile(self):
        global name2
        name2 = asksaveasfilename(initialdir="\\", filetypes=(("Html Files", "*.html"),("All Files","*.*")),
                                  title = "Output path.")
        self.t1.delete("1.0", tk.END)
        self.t1.insert(tk.END, name2)

class App2(tk.Frame):

    def __init__(self, parent, controller, app1):
        tk.Frame.__init__(self, parent) 
        self.app1 = app1
        self.b1 = tk.Button(self, text="Browse", command=self.app1.SaveFile)
        self.b1.grid(row=2, column=4)

app1_inst = App1(parent, controller)
app2_inst = App2(parent, controller, app1_inst)

您正试图通过类引用调用实例方法(要求类的实例存在),例如,您需要创建App1的实例
App1_inst=App1(父级、控制器)
,并将其告知您的app2实例,以便app2可以调用
App1_inst.SaveFile
我应该在哪里键入它?App1的初始化函数还是在类之外?请你再多写些文字好吗?谢谢你的关注。这很有帮助。