I have a strange Treeview behaviour that I can't resolve.
I created a widget using a ttk.Frame
widget and grid a ttk.Treeview
in it as its child. The #0 column shows a directory tree. For each row entry, i.e. tree node, an icon would appear. The option open=True
in the .insert()
method was used.
When this script is executed, all the icons appear in the Treeview as a default.
However, when this same widget is imported into another script and added into a ttk.PanedWindow
widget, the icons in the Treeview does not appear immediately. The icons would only appear when the Treeview row entry is opened by clicking on it. A second click would close the Treeview row entry and the icons would disappear.
I would like the icons in the ttk.Treeview
to appear as a default. How do I do this?
CodePudding user response:
I found the cause of this phenomenon. When I customised the style of the ttk.Treeview
widget in the other script, I discovered the value of the background option was missing a #
symbol. The phenomenon of the ttk.Treeview
icon not appearing as a default is caused by an incorrect syntax in the value of the background
option.
OS: Linux
Test code reproducing the phenomenon caused by incorrect syntax.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.ttk as ttk
class App(ttk.Frame):
def __init__(self, parent=None, *args, **kwargs):
super().__init__(parent)
self.parent = parent
# Create Treeview
self.tree = ttk.Treeview(self, column=('A', 'B'), selectmode='none', height=7)
self.tree.grid(row=0, column=0, sticky='nsew')
# Setup column heading
self.tree.heading('#0', text=' Pic directory', anchor='center')
self.tree.heading('#1', text=' A', anchor='center')
self.tree.heading('#2', text=' B', anchor='center')
# #0, #01, #02 denotes the 0, 1st, 2nd columns
# Setup column
self.tree.column('A', anchor='center', width=100)
self.tree.column('B', anchor='center', width=100)
# Insert image to #0
# change to your file path
self._img = tk.PhotoImage(file="./imagename.png")
self.tree.insert('', 'end', text="#0's text", image=self._img,
value=("A's value", "B's value"))
if __name__ == '__main__':
# Works
root = tk.Tk()
root.geometry('400x180 300 300')
app = App(root)
app.grid(row=0, column=0, sticky='nsew')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()
# Don't Works - syntax error in value of background causing missing icon
# in Treeview.
root = tk.Tk()
root.geometry('400x180 300 300')
style = ttk.Style()
style.configure('Treeview', background='303495') # should be '#303495'
app = App(root)
app.grid(row=0, column=0, sticky='nsew')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()