【问题标题】:How to change the background color of tkinter ttk.PanedWindow widget?如何更改 tkinter ttk.PanedWindow 小部件的背景颜色?
【发布时间】:2021-12-23 13:16:19
【问题描述】:

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

【问题讨论】:

  • 很抱歉,但它确实有效。看到 Button 和 Treeview 之间的黑色空间了吗?那是panedwindow。
  • @Atlas435 那么白色空间叫什么?这将帮助我改写我的问题的标题。
  • 白色区域是树形视图的一部分,而不是窗格窗口。在"my.Treeview" 的样式中设置fieldbackground 并使用另一个主题(当前主题可能不支持某些样式)。
  • style.configure('my.Treeview', background='orange', foreground='grey',fieldbackground='green')style.theme_use('default') 为我工作。
  • @Atlas435 你的权利。在'my.Treeview' 内定义fieldbackground='green' 有效。谢谢。

标签: python tkinter background


【解决方案1】:

@Atlas435 在问题的评论部分中确定,ttk.PanedWindow 的背景确实设置正确。它是ttk.Buttonttk.Treeview 之间的黑色空间。

GUI 中“空白”的颜色实际上是由Treeview 样式布局的fieldbackground 选项控制的空间。尽管 ttk.Style() layoutelement_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() 语句的一个很好的参考是参考Changing Widget Color 上的这个tcl wiki。

@acw1668 和 @Atlas435 在这里被认为回答了我的问题。谢谢

我将这个学习写成一个答案,因为我怀疑 tkinter 用户会面临类似的问题,并希望这个答案可以帮助他们缩短/缓解他们的学习曲线。

【讨论】:

  • 请务必接受您的回答,以便其他用户知道该问题已得到满意回答。
  • @SylvesterKruin SO 需要一天的时间让问题的作者接受他/她自己的答案。
  • 嗯。现在我想起来了,我知道(我只是忘记了),但我不记得我自己是否曾经遇到过麻烦。是在您发布 question 之后的一天(而不是您发布答案之后的一天)?这可以解释为什么我从来没有遇到过这条规则的问题,因为我总是在提出问题至少一天后回答我的问题。很抱歉打扰你:-)!以后我会努力记住这一点的!
  • @SylvesterKruin 这是发布您自己问题的答案的第二天。
猜你喜欢
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 2016-09-10
  • 2014-07-08
  • 2011-10-30
  • 1970-01-01
相关资源
最近更新 更多