英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论