Python 更改tkinter中滚动条的外观(使用ttk样式)

Python 更改tkinter中滚动条的外观(使用ttk样式),python,tkinter,styles,scrollbar,ttk,Python,Tkinter,Styles,Scrollbar,Ttk,我想知道你是否能帮我解决ttk中的样式选项问题。我已经设法将大多数基本的ttk小部件更改为我喜欢的样式。我只会改变滚动条的样式。我找了好几个小时的答案,可惜没有找到 下面是使用滚动条样式选项的示例代码: import tkinter as tk from tkinter import ttk class Gui: def __init__(self,mainframe): #set the style style =

我想知道你是否能帮我解决ttk中的样式选项问题。我已经设法将大多数基本的ttk小部件更改为我喜欢的样式。我只会改变滚动条的样式。我找了好几个小时的答案,可惜没有找到

下面是使用滚动条样式选项的示例代码:

import tkinter as tk                 
from tkinter import ttk

class Gui:
    def __init__(self,mainframe):

        #set the style
        style = ttk.Style()
        style.configure('Horizontal.TScrollbar',background = "blue" )   

        #Create a mainframe
        self.mainframe = mainframe
        self.mainframe.title("example")


        #creating scrollbar frame
        scrl_attr_frame = ttk.Frame(self.mainframe)                            
        scrl_attr_frame.grid(column=0,row=5,sticky="ns")                                           
        scrl_attr_frame.rowconfigure(0, weight=1)                                                   
        attr_canvas = tk.Canvas(scrl_attr_frame)                                                   
        h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
        attr_canvas.configure(xscrollcommand=h_scroll.set)                                       
        attr_canvas.grid(column=0,row=0,sticky="ns")                                                                            
        h_scroll.grid(column=0, row=1,sticky="we") 
        attr_frame = ttk.Frame(attr_canvas)                                                        
        attr_frame.grid(column=0,row=0,sticky="ns")                                                 
        attr_canvas.create_window((0,0),window=attr_frame, anchor='nw')
        attr_frame.bind("<Configure>",lambda event, canvas=attr_canvas : canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200,takefocus=False,highlightthickness=0))#attribute_frame.winfo_height()/20,highlightthickness=0))

        #setup treeview widget
        tree_columns = ("c1", "c2", "c3")

        self.tree = ttk.Treeview(attr_frame,columns=tree_columns, show="headings",takefocus=False)
        self.tree.grid(column=0, row=0, sticky='nsew')

        for head in tree_columns:
            self.tree.heading(head,text=head,anchor="w")


root = tk.Tk()
myapp = Gui(root)
root.mainloop()

非常感谢你的帮助

这在Windows上的tkinter中似乎不可能实现。 下面的答案是这样的:

滚动条文档: 支持的样式字段:

我尝试在我的Windows计算机上同时传递“背景”和“troughcolor”,但都没有成功。我还尝试将该样式应用于常规滚动条: style.configure('TScrollbar',background=“blue”) 我的解决方案都不管用

另外,另一个论坛帖子也同意您不能在此处设置滚动条背景的样式:

看起来您只是想更改Windows主题下水平滚动条的槽。ttk小部件由样式引擎提供的一组元素构成,并使用声明的布局进行组合。在Windows下,样式引擎是Windows视觉样式API,这意味着程序员无法控制用于绘制大多数常用元素的颜色或图像。按钮背景、滚动条槽和按钮以及拇指,甚至滚动条拇指内部绘制的把手都由Windows提供

可以控制应用程序定制,但代价是使应用程序在给定平台上不再是标准的。为此,您必须提供自己的UI元素并定义新的小部件布局。最终,这可以转变为定义自己的主题。ttk库中的tcl脚本提供了很好的示例,甚至有一些完整的(如果是旧的)主题使用位图来声明ttk原始版本中基于图像的主题元素,称为“tile”

在这个特定的示例中,要获得具有自定义颜色背景的Windows水平滚动条,我们需要重新定义布局,以使用Tk绘制元素的滚动条槽。“default”主题中使用的元素可以在中复制,并使用样式配置参数定义,然后由Tk自己绘制,而不是传递给第三方引擎。下面的代码生成这样一个滚动条,它使用vsapi样式引擎提供的标准按钮和拇指,但替换了槽。这个导入的槽理解
troughcolor
样式配置选项,因此我们现在可以定义要使用的颜色。所有使用此样式的滚动条将使用相同的颜色,因为小部件本身不接受troughcolor选项。ie:除非你为每种新颜色定义了新的样式,否则你不能让一个滚动条是蓝色的,另一个是红色的

from tkinter import *
from tkinter.ttk import *

def main():
    app = Tk()
    style = Style()

    # import the 'trough' element from the 'default' engine.
    style.element_create("My.Horizontal.Scrollbar.trough", "from", "default")

    # Redefine the horizontal scrollbar layout to use the custom trough.
    # This one is appropriate for the 'vista' theme.
    style.layout("My.Horizontal.TScrollbar",
        [('My.Horizontal.Scrollbar.trough', {'children':
            [('Horizontal.Scrollbar.leftarrow', {'side': 'left', 'sticky': ''}),
             ('Horizontal.Scrollbar.rightarrow', {'side': 'right', 'sticky': ''}),
             ('Horizontal.Scrollbar.thumb', {'unit': '1', 'children':
                 [('Horizontal.Scrollbar.grip', {'sticky': ''})],
            'sticky': 'nswe'})],
        'sticky': 'we'})])
    # Copy original style configuration and add our new custom configuration option.
    style.configure("My.Horizontal.TScrollbar", *style.configure("Horizontal.TScrollbar"))
    style.configure("My.Horizontal.TScrollbar", troughcolor="red")

    # Create and show a widget using the custom style
    hs = Scrollbar(app, orient="horizontal", style="My.Horizontal.TScrollbar")
    hs.place(x=5, y=5, width=150)
    hs.set(0.2,0.3)

    app.mainloop()

if __name__ == '__main__':
    main()


使用
clam
主题更容易:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style()
style.theme_use('clam')

# list the options of the style
# (Argument should be an element of TScrollbar, eg. "thumb", "trough", ...)
print(style.element_options("Horizontal.TScrollbar.thumb"))

# configure the style
style.configure("Horizontal.TScrollbar", gripcount=0,
                background="Green", darkcolor="DarkGreen", lightcolor="LightGreen",
                troughcolor="gray", bordercolor="blue", arrowcolor="white")

hs = ttk.Scrollbar(root, orient="horizontal")
hs.place(x=5, y=5, width=150)
hs.set(0.2,0.3)

root.mainloop()

如果有人创建了一些东西,并提供了完整的文档和示例,那就太好了,否则什么都不做会更有效率。什么平台?某些平台上的一些本机窗口小部件无法更改。@Stephall我想你是在Mac上,甚至看不到水平滚动条……我在windows 7上工作。谢谢你的时间!伟大的谢谢你的时间和努力。不管结果如何,我都感谢你的帮助。
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style()
style.theme_use('clam')

# list the options of the style
# (Argument should be an element of TScrollbar, eg. "thumb", "trough", ...)
print(style.element_options("Horizontal.TScrollbar.thumb"))

# configure the style
style.configure("Horizontal.TScrollbar", gripcount=0,
                background="Green", darkcolor="DarkGreen", lightcolor="LightGreen",
                troughcolor="gray", bordercolor="blue", arrowcolor="white")

hs = ttk.Scrollbar(root, orient="horizontal")
hs.place(x=5, y=5, width=150)
hs.set(0.2,0.3)

root.mainloop()