在tkinter中从其他类调用按钮函数

时间:2018-11-28 11:59:55

标签: 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)
        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)

1 个答案:

答案 0 :(得分:3)

确定一个更具体的例子:

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的实例。

例如 将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)