python 如何更改tkinter滚动条颜色

nhaq1z21  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(129)

我正在用python做一个文本编辑器,但是我怎么能改变滚动条的颜色呢
我试过用bg,但它不工作

这是我的代码

from tkinter import *
from tkinter import filedialog

# creating our root
root = Tk()
root.title("Text Editor")
root.geometry("1200x640")
root.config(bg="black")

# creating our frame
my_frame = Frame(root)
my_frame.pack(pady=5)

# creating our vertical scroll bar
text_scroll = Scrollbar(my_frame)
text_scroll.pack(side=RIGHT, fill=Y)

# creating our horizantal scroll bar
h_scroll = Scrollbar(my_frame, orient="horizontal")
h_scroll.pack(side=BOTTOM, fill=X)

# creating our text box
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbackground="aqua",         
selectforeground="blue", undo=True, yscrollcommand=text_scroll.set, wrap="none",                             
xscrollcommand=h_scroll.set, bg="#333", fg="aqua")
my_text.pack()

# configure our scroll bar
text_scroll.config(command=my_text.yview)
h_scroll.config(command=my_text.xview)

# main loop
root.mainloop()
1yjd4xko

1yjd4xko1#

Use tkinter.ttk是一个用于设置tkinter小部件样式的模块
我正在用python做一个文本编辑器,但是我怎么能改变滚动条的颜色呢?

  • 添加import ttk命名空间。
  • 添加style = ttk.Style()
  • 添加style.theme_use('classic'
  • 添加style.configure用于垂直和水平。

小片段:

from tkinter import *
from tkinter import filedialog
from tkinter import ttk

# creating our root
root = Tk()
root.title("Text Editor")
root.geometry("1200x640")
root.config(bg="black")

style = ttk.Style()
style.theme_use('classic')
style.configure('Horizontal.TScrollbar',
                background='green',
                bordercolr='red',
                arrowcolor='white'
                )

style.configure('Vertical.TScrollbar',
                background='green',
                bordercolr='red',
                arrowcolor='white'
                )
 
# creating our frame
my_frame = Frame(root)
my_frame.pack(pady=5) 

# creating our vertical scroll bar
text_scroll = ttk.Scrollbar(my_frame, orient="vertical")
text_scroll.pack(side=RIGHT, fill=Y)

# creating our horizantal scroll bar
h_scroll = ttk.Scrollbar(my_frame, orient="horizontal")
h_scroll.pack(side=BOTTOM, fill=X)

# creating our text box
my_text = Text(my_frame, width=97,
               height=25, font=("Helvetica", 16),
               #selectbackground="aqua",         
               #selectforeground="blue",
               undo=True,
               xscrollcommand=h_scroll.set,
               yscrollcommand=text_scroll.set,
               wrap="none",                             
               bg="#333", fg="aqua")
my_text.pack()

# configure our scroll bar
text_scroll.config(command=my_text.yview)
h_scroll.config(command=my_text.xview)
 

# main loop
root.mainloop()

截图:

相关问题