[英]How to change the background color of tkinter ttk.PanedWindow widget?
如何将此 tkinter GUI 中的白色区域更改为不同的颜色?
我尝试通过ttk.Style
进行更改,但是,它不起作用。
下面是我的测试代码。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import tkinter.ttk as ttk
import tkinter as tk
root = tk.Tk()
root['background'] = 'pink'
root.geometry('1200x400+0+100')
# root.rowconfigure(0, weight=1)
# root.columnconfigure(0, weight=1)
style = ttk.Style()
style.configure('my.TPanedwindow', background='black')
style.configure('my.Treeview', background='orange', foreground='grey')
style.configure('my.Treeview.Heading', background='blue', foreground='red')
style.configure('my.Treeview.field', fieldbackground='green')
pw = ttk.PanedWindow(root, cursor='sb_h_double_arrow',
orient=tk.HORIZONTAL,
style='my.TPanedwindow',
width=1000, height=200)
pw.grid(row=0, column=0, ) # sticky='nsew')
b = ttk.Button(pw, text='Test ttk.PanedWindow')
pw.add(b)
def create_treeview(parent):
# Create Treeview
Cols = ('#01', '#02', '#03', '#04', '#05', '#06')
tv = ttk.Treeview(parent, columns=Cols, height=2,
displaycolumn=['#05', '#06', '#01',
'#02', '#03', '#04'],
style='my.Treeview',
selectmode='extended', takefocus=True)
# Setup column & it's headings
tv.column('#0', stretch=0, minwidth=100, width=100, anchor='w')
tv.column('#01', stretch=0, anchor='n', width=70)
tv.column('#02', stretch=0, anchor='n', width=80)
tv.column('#03', stretch=0, anchor='n', width=75)
tv.column('#04', stretch=0, anchor='w')
tv.column('#05', stretch=0, anchor='e', width=80)
tv.column('#06', stretch=0, anchor='n', width=70)
tv.heading('#0', text=' Directory ', anchor='w')
tv.heading('#01', text='#01', anchor='center')
tv.heading('#02', text='#02', anchor='center')
tv.heading('#03', text='#03', anchor='center')
tv.heading('#04', text='#04', anchor='w')
tv.heading('#05', text='#05', anchor='center')
tv.heading('#06', text='#06', anchor='center')
# #0, #01, #02 denotes the 0, 1st, 2nd columns
return tv
tv = create_treeview(pw)
pw.add(tv)
v0 = ('', '', '', '', 'xxx', str('home'), '', '')
tv.insert('', '0', iid='home',
text='Hello',
open=True,
tag='dir',
values=v0
)
root.mainloop()
由于在问题的注释部分鉴定@ Atlas435的背景ttk.PanedWindow
确实正确设置。 它是ttk.Button
和ttk.Treeview
之间的黑色空间。
GUI中“空白”的颜色其实就是Treeview
样式布局的fieldbackground
选项控制的空间。 尽管ttk.Style()
layout
和element_options
方法将fieldbackground
报告为 Treeview 布局的Treeview.field
元素的一个选项,但设置fieldbackground
颜色的正确语法是:
style.configure('Treeview', background='orange', foreground='grey',
fieldbackground='orange')
并不是:
style.configure('Treeview', background='orange', foreground='grey)
style.configure('Treeview.field', fieldbackground='orange')
定义ttk.Style().configure()
语句的一个很好的参考是参考这个 tcl wiki 上的更改控件颜色。
@acw1668 和 @Atlas435 在这里被认为回答了我的问题。 谢谢
我已经写了这个学习作为答案,因为我怀疑 tkinter 用户会面临类似的问题,并希望这个答案可以帮助他们缩短/缓解他们的学习曲线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.