替换 tkinter.Text 插入方法
Replacing the tkinter.Text insert method
我试图替换(在运行时)继承的 insert() 方法用于 tkinter.Text[的子class。替换方法在调用父 class' (tkinter.Text) insert() 方法之前执行几行代码。但是,在运行时 python 显示以下错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1885, in __call__
return self.func(*args)
File ".../expts/test.py", line 20, in callback
text.insert("1.0", "howdy")
File ".../expts/test.py", line 14, in new_insert
tk.Text.insert(text, index, chars, *args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 3740, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
AttributeError: type object 'Text' has no attribute 'tk'
以下代码是我主要代码的简化版。
import tkinter as tk
import types
class TextChild(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
root = tk.Tk()
text = TextChild(root)
text.pack()
def new_insert(text, index, chars, *args):
print("Does some work...")
tk.Text.insert(text, index, chars, *args)
text.insert = types.MethodType(new_insert, tk.Text)
def callback():
global text
text.insert("1.0", "howdy")
button = tk.Button(master=root,text="Button", command=callback)
button.pack()
root.mainloop()
在继承的class:
里面更容易做到
class TextChild(tk.Text):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def insert(self, index, chars, *args):
print("Does some work ...")
# call original insert()
super().insert(index, chars, *args)
为了解决您原来的方法,您混淆了 types.MethodType
的参数。参数是方法和对象。
text.insert = types.MethodType(new_insert, text)
我试图替换(在运行时)继承的 insert() 方法用于 tkinter.Text[的子class。替换方法在调用父 class' (tkinter.Text) insert() 方法之前执行几行代码。但是,在运行时 python 显示以下错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1885, in __call__
return self.func(*args)
File ".../expts/test.py", line 20, in callback
text.insert("1.0", "howdy")
File ".../expts/test.py", line 14, in new_insert
tk.Text.insert(text, index, chars, *args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 3740, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
AttributeError: type object 'Text' has no attribute 'tk'
以下代码是我主要代码的简化版。
import tkinter as tk
import types
class TextChild(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
root = tk.Tk()
text = TextChild(root)
text.pack()
def new_insert(text, index, chars, *args):
print("Does some work...")
tk.Text.insert(text, index, chars, *args)
text.insert = types.MethodType(new_insert, tk.Text)
def callback():
global text
text.insert("1.0", "howdy")
button = tk.Button(master=root,text="Button", command=callback)
button.pack()
root.mainloop()
在继承的class:
里面更容易做到class TextChild(tk.Text):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def insert(self, index, chars, *args):
print("Does some work ...")
# call original insert()
super().insert(index, chars, *args)
为了解决您原来的方法,您混淆了 types.MethodType
的参数。参数是方法和对象。
text.insert = types.MethodType(new_insert, text)