首页 > 解决方案 > 从 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)

标签: python-3.xooptkinter

解决方案


确定一个更具体的例子:

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)

推荐阅读