如何将自定义对象存储在类似于Tkinter的Variable对象的变量中

huangapple go评论68阅读模式
英文:

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 &quot;repr&quot;
        

import tkinter as tk

root = tk.Tk()
variable = tk.Variable()
variable.set(Test())
print(variable.get())
print(type(variable.get()))


#output
repr
&lt;class &#39;str&#39;&gt;

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=&quot;write&quot;, callback=callback)

tk.Button(root, text=&quot;Write&quot;, command=lambda: variable.set(Test(randint(0,10)))).pack()
root.mainloop()

huangapple
  • 本文由 发表于 2023年6月1日 17:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定