你能在Python中删除Tkinter Scrollbar小部件上的箭头吗?

时间:2018-02-09 03:22:51

标签: python python-2.7 tkinter

是否可以删除Tkinter上滚动条小部件上的箭头,并且只有矩形滑块。滚动条当前附加到列表框,不需要箭头。

如果它不可用,有没有办法使用Tkinter手动创建滚动条?

注意:目前使用的是Python 2.7 谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用ttk.Style创建不带箭头的自定义滚动条布局:

style.layout('arrowless.Vertical.TScrollbar', 
             [('Vertical.Scrollbar.trough',
               {'children': [('Vertical.Scrollbar.thumb', 
                              {'expand': '1', 'sticky': 'nswe'})],
                'sticky': 'ns'})])

这是Linux中默认ttk主题中的原始Vertical.TScrollbar布局:

[('Vertical.Scrollbar.trough',
  {'children': [('Vertical.Scrollbar.uparrow', {'side': 'top', 'sticky': ''}),
    ('Vertical.Scrollbar.downarrow', {'side': 'bottom', 'sticky': ''}),
    ('Vertical.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})],
   'sticky': 'ns'})]

您可以在其中查看向上和向下箭头。以下是列表框的示例:

import ttk
import Tkinter as tk

root = tk.Tk()

listbox = tk.Listbox(root)
for i in range(20):
    listbox.insert('end', 'item %i' %i)

style = ttk.Style(root)
# create new scrollbar layout
style.layout('arrowless.Vertical.TScrollbar', 
         [('Vertical.Scrollbar.trough',
           {'children': [('Vertical.Scrollbar.thumb', 
                          {'expand': '1', 'sticky': 'nswe'})],
            'sticky': 'ns'})])
# create scrollbar without arrows           
scroll = ttk.Scrollbar(root, orient='vertical', command=listbox.yview, 
                       style='arrowless.Vertical.TScrollbar')
listbox.configure(yscrollcommand=scroll.set)

listbox.pack(side='left', fill='both', expand=True)
scroll.pack(side='right', fill='y')
root.mainloop()

result