TreeView 样式不适用于 tkinter
TreeView style does not apply in tkinter
我在根 window 中设计了一个带有 tkinter 的按钮,当我单击该按钮时,应该会出现一个带有 treeView 的新 window。我已经为 treeView 定义了样式,但它不适用。
完整代码如下
import tkinter as tk
from tkinter import ttk
import pandas as pd
root = tk.Tk()
style1 = ttk.Style()
style1.configure("mystyle1.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11, 'bold')) # Modify the font of the body
style1.configure("mystyle1.Treeview.Heading", font=('Calibri', 10,'bold'),foreground="white", background="blue") # Modify the font of the headings
style1.layout("mystyle1.Treeview", [('mystyle1.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
def displaytree():
app = tk.Tk()
sample = {"File Name":[f"file_{i}" for i in range(5)],
'Sheet Name': [f"sheet_{i}" for i in range(5)],
'Number Of Rows': [f"row_{i}" for i in range(5)],
'Number Of Columns': [f"col_{i}" for i in range(5)]
}
df = pd.DataFrame(sample)
cols = list(df.columns)
treeV = ttk.Treeview(app, style="mystyle1.Treeview")
treeV.pack()
treeV["columns"] = cols
for i in cols:
treeV.column(i, anchor="w")
treeV.heading(i, text=i, anchor='w')
for index, row in df.iterrows():
treeV.insert("","end",text=index,values=list(row))
app.mainloop()
bt1 = tk.Button(root, text="DISPLAY TREE VIEW", font="Times 12 bold", activebackground="white",
activeforeground="green",width=16,height=1,bg="green",fg="white", command=displaytree)
bt1.pack()
root.mainloop()
我错过了什么?
预先感谢您的帮助
不要调用 tk.Tk() 两次,而是使用 Toplevel。
import tkinter as tk
from tkinter import ttk
import pandas as pd
root = tk.Tk()
style1 = ttk.Style()
style1.configure("mystyle1.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11, 'bold')) # Modify the font of the body
style1.configure("mystyle1.Treeview.Heading", font=('Calibri', 10,'bold'),foreground="white", background="blue") # Modify the font of the headings
style1.layout("mystyle1.Treeview", [('mystyle1.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
def displaytree():
app = tk.Toplevel()
sample = {"File Name":[f"file_{i}" for i in range(5)],
'Sheet Name': [f"sheet_{i}" for i in range(5)],
'Number Of Rows': [f"row_{i}" for i in range(5)],
'Number Of Columns': [f"col_{i}" for i in range(5)]
}
df = pd.DataFrame(sample)
cols = list(df.columns)
treeV = ttk.Treeview(app, style="mystyle1.Treeview")
treeV.pack()
treeV["columns"] = cols
for i in cols:
treeV.column(i, anchor="w")
treeV.heading(i, text=i, anchor='w')
for index, row in df.iterrows():
treeV.insert("","end",text=index,values=list(row))
app.mainloop()
bt1 = tk.Button(root, text="DISPLAY TREE VIEW", font="Times 12 bold", activebackground="white",
activeforeground="green",width=16,height=1,bg="green",fg="white", command=displaytree)
bt1.pack()
root.mainloop()
我在根 window 中设计了一个带有 tkinter 的按钮,当我单击该按钮时,应该会出现一个带有 treeView 的新 window。我已经为 treeView 定义了样式,但它不适用。 完整代码如下
import tkinter as tk
from tkinter import ttk
import pandas as pd
root = tk.Tk()
style1 = ttk.Style()
style1.configure("mystyle1.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11, 'bold')) # Modify the font of the body
style1.configure("mystyle1.Treeview.Heading", font=('Calibri', 10,'bold'),foreground="white", background="blue") # Modify the font of the headings
style1.layout("mystyle1.Treeview", [('mystyle1.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
def displaytree():
app = tk.Tk()
sample = {"File Name":[f"file_{i}" for i in range(5)],
'Sheet Name': [f"sheet_{i}" for i in range(5)],
'Number Of Rows': [f"row_{i}" for i in range(5)],
'Number Of Columns': [f"col_{i}" for i in range(5)]
}
df = pd.DataFrame(sample)
cols = list(df.columns)
treeV = ttk.Treeview(app, style="mystyle1.Treeview")
treeV.pack()
treeV["columns"] = cols
for i in cols:
treeV.column(i, anchor="w")
treeV.heading(i, text=i, anchor='w')
for index, row in df.iterrows():
treeV.insert("","end",text=index,values=list(row))
app.mainloop()
bt1 = tk.Button(root, text="DISPLAY TREE VIEW", font="Times 12 bold", activebackground="white",
activeforeground="green",width=16,height=1,bg="green",fg="white", command=displaytree)
bt1.pack()
root.mainloop()
我错过了什么? 预先感谢您的帮助
不要调用 tk.Tk() 两次,而是使用 Toplevel。
import tkinter as tk
from tkinter import ttk
import pandas as pd
root = tk.Tk()
style1 = ttk.Style()
style1.configure("mystyle1.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11, 'bold')) # Modify the font of the body
style1.configure("mystyle1.Treeview.Heading", font=('Calibri', 10,'bold'),foreground="white", background="blue") # Modify the font of the headings
style1.layout("mystyle1.Treeview", [('mystyle1.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
def displaytree():
app = tk.Toplevel()
sample = {"File Name":[f"file_{i}" for i in range(5)],
'Sheet Name': [f"sheet_{i}" for i in range(5)],
'Number Of Rows': [f"row_{i}" for i in range(5)],
'Number Of Columns': [f"col_{i}" for i in range(5)]
}
df = pd.DataFrame(sample)
cols = list(df.columns)
treeV = ttk.Treeview(app, style="mystyle1.Treeview")
treeV.pack()
treeV["columns"] = cols
for i in cols:
treeV.column(i, anchor="w")
treeV.heading(i, text=i, anchor='w')
for index, row in df.iterrows():
treeV.insert("","end",text=index,values=list(row))
app.mainloop()
bt1 = tk.Button(root, text="DISPLAY TREE VIEW", font="Times 12 bold", activebackground="white",
activeforeground="green",width=16,height=1,bg="green",fg="white", command=displaytree)
bt1.pack()
root.mainloop()