英文:
how to return a event from the _proxy method?
问题
In Python 3.8中,我正在尝试每当 tkinter Text 小部件中的内容更改时获取回调。
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self._proxy)
self.scrollbar_ver = tk.Scrollbar(self, orient=tk.VERTICAL)
def _proxy(self, command, *args):
if command == 'get' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
if command == 'delete' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
if command in ('insert', 'delete', 'replace'):
self.event_generate('<<TextModified>>', data=f"Command: {command}, Args: {args}")
return result
def callback(event: tk.Event):
print(event.keysym)
print(event.data) # This will print the additional info you attached.
root = tk.Tk()
text = CustomText(root)
text.pack()
text.bind('<<TextModified>>', callback)
root.mainloop()
我想从事件中获取一些信息,例如 keycode 或 keysym。但这似乎是不可能的,因为事件在生成时没有任何信息。您可以在生成 TextModified 事件时附加信息,像这样做。
英文:
In python 3.8,I am trying to get a callback every time something is changed in the tkinter Text widget.
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self._proxy)
self.scrollbar_ver = tk.Scrollbar(self, orient=tk.VERTICAL)
def _proxy(self, command, *args):
if command == 'get' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
if command == 'delete' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
if command in ('insert', 'delete', 'replace'):
self.event_generate('<<TextModified>>')
return result
def callback(event: tk.Event):
print(event.keysym)
root = tk.Tk()
text = CustomText(root)
text.pack()
text.bind('<<TextModified>>', callback)
root.mainloop()
I want to get some info from the event, such as keycode or keysym. But that's impossible because the event is generated without any info. How can I attach the info when I generate the TextModified event?
答案1
得分: 0
以下是您要翻译的内容:
You cannot directly send this kind of information through the generated event.
However, you can store in an attribute of the widget the desired information about the executed command and retrieve it in the callback:
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self._proxy)
self.event_info = "" # attribute to store the info
self.scrollbar_ver = tk.Scrollbar(self, orient=tk.VERTICAL)
def _proxy(self, command, *args):
if command == 'get' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
if command == 'delete' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
if command in ('insert', 'delete', 'replace'):
self.event_info = command # <- store whatever info you need about the command there
self.event_generate('<<TextModified>>')
return result
def callback(event: tk.Event):
print(event.widget.event_info) # retrieve the info about the command
root = tk.Tk()
text = CustomText(root)
text.pack()
text.bind('<<TextModified>>', callback)
root.mainloop()
我已经将代码部分翻译好,如有需要,请继续。
英文:
You cannot directly send this kind of information through the generated event.
However, you can store in an attribute of the widget the desired information about the executed command and retrieve it in the callback:
import tkinter as tk
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
self._orig = self._w + "_orig"
self.tk.call("rename", self._w, self._orig)
self.tk.createcommand(self._w, self._proxy)
self.event_info = "" # attribute to store the info
self.scrollbar_ver = tk.Scrollbar(self, orient=tk.VERTICAL)
def _proxy(self, command, *args):
if command == 'get' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
if command == 'delete' and args == ('sel.first', 'sel.last') and not self.tag_ranges('sel'):
return
cmd = (self._orig, command) + args
result = self.tk.call(cmd)
if command in ('insert', 'delete', 'replace'):
self.event_info = command # <- store whatever info you need about the command there
self.event_generate('<<TextModified>>')
return result
def callback(event: tk.Event):
print(event.widget.event_info) # retrieve the info about the command
root = tk.Tk()
text = CustomText(root)
text.pack()
text.bind('<<TextModified>>', callback)
root.mainloop()
I am aware this is not exactly what was asked in the question since this is not a keysym or keycode. When the proxy function is called, we do not know what triggered the command so it is difficult to get the exact keysym/keycode. However, I do not know what kind of callback the OP wants to implement, so maybe the necessary information for that can be determined from the command arguments, e.g. what characters were added/deleted.
If what is really needed is a keysym/keycode, then one has to use a binding to <Key>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论