英文:
How to store custom object in Variable similar to Tkinter's Variable object
问题
在Tkinter中,我尝试使用Variable
对象来存储自定义对象。然而,从变量中检索始终返回自定义对象的__repr__()
函数返回的str
对象,如下面的代码片段所示。
class Test:
def __init__(self):
pass
def __repr__(self):
return "repr"
import tkinter as tk
root = tk.Tk()
variable = tk.Variable()
variable.set(Test())
print(variable.get())
print(type(variable.get()))
#output
repr
<class 'str'>
由于我正在使用trace_add
和一个回调函数,该回调函数然后使用我存储在Variable
中的对象,因此我需要它来存储变量而不仅仅是字符串表示。然而,tkinter的现有Var
对象不包括用于对象的对象。是否存在一种现有的方法来实现这一点,或者是否需要子类化Variable
或使用单独的存储机制并利用跟踪来触发访问?
英文:
In Tkinter, I attempted to use the Variable
object to store custom objects. However, retrieving from the variable always returns a str
object of the custom object's __repr__()
function as can be seen from the snippet below.
class Test:
def __init__(self):
pass
def __repr__(self):
return "repr"
import tkinter as tk
root = tk.Tk()
variable = tk.Variable()
variable.set(Test())
print(variable.get())
print(type(variable.get()))
#output
repr
<class 'str'>
As I am using the Variable with trace_add
and a callback function which then utilises the object I stored in the Variable
, I would need it to store the variable rather than just the string repr. However, tkinter's existing Var
objects do not include one for objects. Is there an existing way to accomplish this, or will this require subclassing Variable
or using a separate storage mechanism and utilising trace to trigger access?
答案1
得分: 0
找到了一种解决方法,通过子类化 tk.Variable
并覆盖/创建函数来实现。诚然,我对追踪函数的工作方式了解不够,不知道是否有更好的方法来做到这一点,但这对我的需求有效。
class Test:
def __init__(self, value):
self.value = value
pass
import tkinter as tk
from random import randint
class CustomVar(tk.Variable):
def __init__(self):
super().__init__()
self.current = None
def set(self, value: object):
self.current = value
# 调用 super 的 set 方法以触发写入模式的追踪
super().set(value)
def get(self):
# 同样用于触发读取模式追踪
super().get()
return self.current
def callback(*args):
print(variable.get())
print(type(variable.get()))
print(variable.get().value)
root = tk.Tk()
variable = CustomVar()
variable.trace_add(mode="write", callback=callback)
tk.Button(root, text="Write", command=lambda: variable.set(Test(randint(0,10)))).pack()
root.mainloop()
英文:
Found a solution by subclassing tk.Variable
and overwriting/creating functions. Admittedly, I don't know enough about how the trace function works to know if there is a better way to do it, but this has worked for my needs.
class Test:
def __init__(self, value):
self.value = value
pass
import tkinter as tk
from random import randint
class CustomVar(tk.Variable):
def __init__(self):
super().__init__()
self.current = None
def set(self, value: object):
self.current = value
# calling set on super to trigger write mode traces
super().set(value)
def get(self):
# similarly for triggering read mode traces
super().get()
return self.current
def callback(*args):
print(variable.get())
print(type(variable.get()))
print(variable.get().value)
root = tk.Tk()
variable = CustomVar()
variable.trace_add(mode="write", callback=callback)
tk.Button(root, text="Write", command=lambda: variable.set(Test(randint(0,10)))).pack()
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论