TclError在将字典解包到treeview.insert时出现未知选项(tkinter)

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

TclError unknown option when unpacking a dictionary into treeview.insert (tkinter)

问题

以下是您要翻译的内容:

I have treeview tree built like so:

tree = ttk.Treeview(self.treeview_frame, columns=columns,
                                 yscrollcommand=scroll_y.set,
                                 xscrollcommand=scroll_x.set,
                                 show="headings")

for name in columns:
    tree.column(name, anchor=tk.W, width=150, stretch=True)
    tree.heading(name, text=name, anchor=tk.CENTER)

Where columns for this example would be ["Annotation Name"].

I have a dictionary input_dict:

{'parent': '',
 'iid': 'Shell-local',
 'text': 'Shell-local (Ann.)',
 'image': None,
 'values': ('Shell Annotation',),
 'open': True,
 'tags': [],
 'index': 'end'}

Which matches the required keyword arguments for treeview.insert(), but when executing the following:
self.tree.insert(**input_dict)

The below error occurs:

File "C:\ProgramData\Miniconda3\envs\aat\lib\tkinter\ttk.py", line 1361, in insert
    res = self.tk.call(self._w, "insert", parent, index,
_tkinter.TclError: unknown option "Shell Annotation"

I've tries replacing the backspace character with '\b':

new_values = tuple([str(val).replace(" ", "\b") for val in input_dict["values"]])
input_dict["values"] = new_values

But...:

File "C:\ProgramData\Miniconda3\envs\aat\lib\tkinter\ttk.py", line 1361, in insert
    res = self.tk.call(self._w, "insert", parent, index,
_tkinter.TclError: unknown option "ShelAnnotation"

The reason I don't just assign the input arguments like this self.tree.insert(index=input_dict["index"],...) is because the input_dict is the result of __dict__ of a dataclass, and I want the code to be flexible with changes in the dataclass.

Anyone knows what causes these errors?

英文:

I have treeview tree built like so:

tree = ttk.Treeview(self.treeview_frame, columns=columns,
                                 yscrollcommand=scroll_y.set,
                                 xscrollcommand=scroll_x.set,
                                 show="headings")

for name in columns:
    tree.column(name, anchor=tk.W, width=150, stretch=True)
    tree.heading(name, text=name, anchor=tk.CENTER)

Where columns for this example would be ["Annotation Name"].

I have a dictionary input_dict:

{'parent': '',
 'iid': 'Shell-local',
 'text': 'Shell-local (Ann.)',
 'image': None,
 'values': ('Shell Annotation',),
 'open': True,
 'tags': [],
 'index': 'end'}

Which matches the required keyword arguments for treeview.insert(),
but when executing the following:
self.tree.insert(**input_dict)

The below error occurs:

File "C:\ProgramData\Miniconda3\envs\aat\lib\tkinter\ttk.py", line 1361, in insert
    res = self.tk.call(self._w, "insert", parent, index,
_tkinter.TclError: unknown option "{Shell Annotation}"

I've tries replacing the backspace character with '\b':

new_values = tuple([str(val).replace(" ", "\b") for val in input_dict["values"]])
input_dict["values"] = new_values

But...:

File "C:\ProgramData\Miniconda3\envs\aat\lib\tkinter\ttk.py", line 1361, in insert
    res = self.tk.call(self._w, "insert", parent, index,
_tkinter.TclError: unknown option "ShelAnnotation"

The reason I don't just assign the input arguments like this self.tree.insert(index=input_dict["index"],...) is because the input_dict is the result of __dict__ of a dataclass, and I want the code to be flexible with changes in the dataclass.

Anyone knows what causes these errors?

答案1

得分: 1

'image = None' 是有责任的。
input_dict 弹出键 image 后,代码可以正常工作。

英文:

Looks like 'image' = None is responsible.
After popping the key 'image' from input_dict the code works.

huangapple
  • 本文由 发表于 2023年2月16日 15:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468938.html
匿名

发表评论

匿名网友

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

确定