更改 tkinter 中滚动条的外观(使用 ttk 样式) [英] Changing the appearance of a Scrollbar in tkinter (using ttk styles)

查看:32
本文介绍了更改 tkinter 中滚动条的外观(使用 ttk 样式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您是否可以帮助我解决 ttk 中的样式选项问题.我已经设法将大多数基本的 ttk 小部件更改为我喜欢的样式.我只坚持改变滚动条的样式.我已经搜索了几个小时来寻找答案,不幸的是无济于事.

I was wondering if you could help me with a style options issue in ttk. I've managed to change most of the basic ttk widgets to the style of my preference. I'm only stuck at changing the style of a scrollbar. I've searched for hours looking for an answer, unfortunately to no avail.

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

Here's a sample code using the scrollbar style option:

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()

我也尝试了几种组合,包括;

I also tried several combinations including;

style.configure('TScrollbar',background='blue') 

#and
style.configure('CustomScroll.Horizontal.TScrollbar',background='blue')

#in combination with
h_scroll = ttk.Scrollbar(scrl_attr_frame,orient="horizontal", command=attr_canvas.xview)
h_scroll['style'] = "CustomScroll.Horizontal.TScrollbar" 

非常感谢您的帮助!

推荐答案

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

It looks like you just want to change the trough for a horizontal scrollbar under the Windows theme. The ttk widgets are constructed from a set of elements provided by a styling engine and combined using the declared layout. Under Windows the styling engine is the Windows Visual Styles API which means the programmer doesn't have any control over the colours or images used to draw most of the common elements. The button background, scrollbar trough and buttons and the thumb and even the grip drawn inside the scrollbar thumb are all provided by Windows.

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

It is possible to take control of this for application customization but at a cost of making your application no longer look standard on the given platform. To do this you have to provide your own UI elements and define new widget layouts. Ultimately this can turn into defining your own theme. The tcl scripts in the ttk library provide good examples to follow and there are even some complete (if old) themes using bitmaps to declare image based theme elements in the original version of ttk which was called 'tile'.

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

In this specific example to get a Windows horizontal scrollbar with a custom coloured background we need to redefine the layout to use the scrollbar trough from the Tk drawn elements. The elements used in the 'default' theme can be copied in and are defined using style configuration parameters and are then drawn by Tk itself and not passed off to a third party engine. The following code generates a scrollbar like this which uses the standard buttons and thumb provided by the vsapi styling engine but replaces the trough. This imported trough understands the troughcolor style configuration option and so we can define a colour to use now. All scrollbars using this style will use the same colour as the widget itself will not accept a troughcolor option. ie: you can't have one scrollbar be blue and another be red unless you define a new style for each new colour.

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()

这篇关于更改 tkinter 中滚动条的外观(使用 ttk 样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆