如何將此 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()

uj5u.com熱心網友回復:
正如問題評論部分中的@Atlas435 所指出的那樣,背景ttk.PanedWindow確實設定正確。它是ttk.Button和之間的黑色空間ttk.Treeview。
GUI中“空白”的顏色其實fieldbackground就是Treeview樣式布局的選項控制的空間。盡管ttk.Style() layout和element_options方法報告fieldbackground為Treeview.fieldTreeview 布局元素的一個選項,但設定顏色的正確語法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 用戶會面臨類似的問題,并希望這個答案可以幫助他們縮短/緩解他們的學習曲線。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358532.html
