【问题标题】:Tkinter how to correctly style a combobox scrollbarTkinter 如何正确设置组合框滚动条的样式
【发布时间】:2020-09-15 03:18:55
【问题描述】:

我正在尝试设置组合框小部件滚动条的样式。我想设置槽的颜色和大小。 我已经能够通过更改箭头大小参数来设置大小,如下面的代码所示。这是不可取的,因为它会更改所有小部件的所有垂直滚动条。我想针对特定的小部件。

import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.title('Default Demo')
        self.geometry('420x200')

        style = ttk.Style()
        style.configure('my.TCombobox', arrowsize=30)
        style.configure('Vertical.TScrollbar', arrowsize=28)
        # style.configure('my.TCombobox.Vertical.TScrollbar', arrowsize=28)

        values = []
        for idx in range(1, 50):
            values.append(f'Testing-{idx}')

        cbo = ttk.Combobox(self, values=values, style='my.TCombobox')
        cbo.grid(ipady=5)


def main():
    app = App()
    app.mainloop()


if __name__ == '__main__':
    main()

【问题讨论】:

  • 如果你想要不同的风格,那么你需要创造一些。您可以做多个服装样式的滚动条并将它们添加到您想要的小部件中。也许我错过了这里的问题。
  • 你还需要看看这个适合你的颜色 stackoverflow.com/questions/28375591/…>
  • @Atlas435,我正在尝试在组合框下拉菜单中设置滚动条的样式。我不知道如何解决它。我尝试了上面注释掉的代码,但它什么也没做。

标签: python tkinter ttk


【解决方案1】:

组合框的滚动条不能通过python接口直接访问,但是可以通过tcl解释器改变它的样式。

首先,为组合框的下拉列表命名:

self.tk.eval('set popdown [ttk::combobox::PopdownWindow %s]' % cbo)

然后改变滚动条的样式:

self.tk.eval(f'$popdown.f.sb configure -style my.TCombobox.Vertical.TScrollbar')

完整示例:

import tkinter as tk
import tkinter.ttk as ttk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.title('Default Demo')
        self.geometry('420x200')

        style = ttk.Style()
        style.configure('my.TCombobox', arrowsize=30)
        style.configure('my.TCombobox.Vertical.TScrollbar', arrowsize=28)

        values = []
        for idx in range(1, 50):
            values.append(f'Testing-{idx}')

        cbo = ttk.Combobox(self, values=values, style='my.TCombobox')
        cbo.grid(ipady=5)

        self.tk.eval('set popdown [ttk::combobox::PopdownWindow %s]' % cbo)
        self.tk.eval(f'$popdown.f.sb configure -style my.TCombobox.Vertical.TScrollbar')

        ttk.Scrollbar(self, orient='vertical').grid(row=0, column=1, sticky='ns')

if __name__ == '__main__':
    app = App()
    app.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多