python-3.x - 从 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)
解决方案
确定一个更具体的例子:
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)
推荐阅读
- android - 如何确保应用程序兼容 64 位?
- java - 用于日志分析的 Map Reduce 作业未在 Hadoop 2.7.3 伪分布式模式下运行
- c# - 如何在 Xamarin Android 中将 C# 对象转换为 Java.lang 对象?
- php - 在三列中显示php数组,第一行一个元素,第二行和第三行两个元素
- reactjs - 自定义 ag-grid-filter 使用下拉列表使用 react-redux 过滤特定列
- jquery - Jquery ajax 发布发起者 url 与实际发布请求 url 不同
- mysql - Rails 在 MYSql 安装后寻找 MySQL 而不是 Postgres
- r - 将视频文件从R中的外部源转换为像素
- objective-c - Google 移动广告 SDK 不支持与 armv7s 的链接。从构建设置中的“ARCHS”(架构)中删除 armv7s
- angular - 离子如何从提供者那里获得承诺响应?