使用 ttk 样式更改 Tkinter 中滚动条的外观
滚动条用于将一定数量的文本或字符包裹在框架或窗口中。它提供了一个文本小组件来容纳用户想要的任意数量的字符。
滚动条可以分为两种类型:水平滚动条和垂直滚动条。
文本小组件中字符数发生变化时,滚动条的长度也会随之变化。我们可以使用 ttk.Scrollbar来配置滚动条的样式。Ttk 提供了许多内置功能和属性,可用于配置滚动条。
示例
在此示例中,我们将在一个文本小组件中添加一个垂直滚动条。我们将使用 ttk 样式主题来定制滚动条的外观。这里我们使用了“经典”主题。有关 ttk 主题的完整列表,请参考此链接。
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry of Tkinter Frame
win.geometry("700x250")
style=ttk.Style()
style.theme_use('classic')
style.configure("Vertical.TScrollbar", background="green", bordercolor="red", arrowcolor="white")
# Create a vertical scrollbar
scrollbar = ttk.Scrollbar(win, orient='vertical')
scrollbar.pack(side=RIGHT, fill=BOTH)
# Add a Text Widget
text = Text(win, width=15, height=15, wrap=CHAR,
yscrollcommand=scrollbar.set)
for i in range(1000):
text.insert(END, i)
text.pack(side=TOP, fill=X)
# Configure the scrollbar
scrollbar.config(command=text.yview)
win.mainloop()输出
运行上面的代码将显示一个带有文本小组件和自定义垂直滚动条的窗口。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP