如何更改 Tkinter ScrolledText 小部件的滚动条颜色?

发布于 2024-11-28 08:33:11 字数 565 浏览 0 评论 0原文

我在 Tkinter GUI 中使用模块 ScrolledText

我希望更改 ScrolledText 小部件中包含的 Scrollbar 的颜色,但我遇到了一些困难。

我的语法是正确的(根据文档)。

Box = ScrolledText(root)
Box.vbar.config(troughcolor = 'red', bg = 'blue')
Box.pack()

然而,滚动条仍然具有标准的灰色。

我知道语法是正确的,因为执行其他操作,例如:

 Box.vbar.config(cursor = 'target')   

... 完全按照其应有的方式工作。

但是,尝试更改浮雕边框宽度或颜色似乎没有任何效果。
为什么会发生这种情况?


规格:

Python 2.7.1
Tkinter
Windows 7
闲置的

I'm using the module ScrolledText in a Tkinter GUI.

I wish to change the colour of the Scrollbar encompassed in the ScrolledText widget, but I'm having some difficulty.

My syntax is correct (according to documentation).

Box = ScrolledText(root)
Box.vbar.config(troughcolor = 'red', bg = 'blue')
Box.pack()

However, the scrollbar still has the standard grey colour.

I know the syntax is correct, because doing other things such as:

 Box.vbar.config(cursor = 'target')   

...works exactly as it should.

However, attempting to change the relief, borderwidth or colors don't seem to have any effect.
Why is this happening?


Specs:

Python 2.7.1
Tkinter
Windows 7
IDLE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不吻晚风 2024-12-05 08:33:11

Tk 滚动条小部件 (vbar) 是 Windows 中的本机滚动条。它的外观取决于 Windows 主题。如果重要的话,考虑切换工具包;我知道 PyQt4 可以让你在 Windows 上设置滚动条的样式。

The Tk Scrollbar widget (vbar) is a native scrollbar in Windows. Its appearance depends on the Windows theme. Consider switching toolkits if it matters; I know PyQt4 will let you style the scrollbar on Windows.

很快妥协 2024-12-05 08:33:11

据我所知,在 Windows 上格式化滚动条确实很困难。下面是我编写的滚动条程序的一些代码。我正在尝试开发一些应该具有这些选项的软件,但在我看来,这就像一个小部件的大量代码,其主要用途 Tkinter 做得更好。然而,该程序应该允许您将图像插入箭头、槽和滑块以及更改它们的颜色。

from tkinter import *

class App:
    def __init__(self):
        self.t = Text(root)
        self.t.place(x = 50,y = 50,width = 400,height = 400)
        for i in range(300):
            self.t.insert(END,str(i))
            self.t.insert(END,"\n")
        self.c = Canvas(root)
        self.c.place(x = 450,y = 50,width = 20,height = 400)
        self.trough = Canvas(self.c,bg = "red")
        self.slider = Label(self.c,bg = "grey")
        self.slidery = 20
        self.sliderh = 200
        self.trough.place(x = 0,y = 20,width = 20,height = 360)
        self.slider.place(x = 0,y = 20,width = 20,height = 200)
        self.up = Label(self.c,bg = "purple")
        self.down = Label(self.c,bg = "purple")
        self.up.place(x = 0,y = 0,width = 20,height = 20)
        self.down.place(x = 0,y = 380,width = 20,height = 20)
        self.up.bind("<Button-1>",self.up_view)
        self.down.bind("<Button-1>",self.down_view)
        self.slider.bind("<Button-1>",self.get_slider)
        self.slider.bind("<B1-Motion>",self.move_slider)
        self.slider.bind("<ButtonRelease-1>",self.release_slider)
        self.trough.bind("<Button-1>",self.next_page)
        self.config_slider()

    def next_page(self,event):
        y = event.y
        top = self.slidery - 20
        bottom = self.slidery + self.sliderh - 20
        coords = self.get_coords()
        if y < top:
            self.up_page(coords)
        if y > bottom:
            self.down_page(coords)

    def up_page(self,coords):
        up = -(coords[1] - coords[0])
        self.t.yview(SCROLL,-1,"pages")
        self.pos_slider()

    def down_page(self,coords):
        down = coords[1] - coords[0]
        self.t.yview(SCROLL,1,"pages")
        self.pos_slider()

    def pos_slider(self):
        coords = self.get_coords()
        self.slidery = (coords[0]/coords[2] * 360) + 20
        self.slider.place(y = self.slidery)

    def config_slider(self):
        coords = self.get_coords()
        top = coords[0]
        bottom = coords[1]
        last = coords[2]
        last -= 1
        if bottom-top < last:
            f = int((bottom-top)/last * 360)
        else:
            f = 360
        if f < 10:
            f = 10
        self.sliderh = f
        self.slider.place(height = f,y = self.slidery)

    def get_coords(self):
        root.update()
        top = self.t.index("@0,0")
        bottom = self.t.index("@0,%d" %self.t.winfo_height())
        last = self.t.index(END)
        t = top.split(".")
        b = bottom.split(".")
        l = last.split(".")
        top = int(t[0])
        bottom = int(b[0])
        last = int(l[0])
        return [top,bottom,last]

    def up_view(self,event):
        self.t.yview(SCROLL,-2,"units")
        self.pos_slider()

    def down_view(self,event):
        self.t.yview(SCROLL,2,"units")
        self.pos_slider()

    def get_slider(self,event):
        self.y = event.y_root
        self.config_slider()

    def move_slider(self,event):
        y = event.y_root - self.y
        y += self.slidery
        if y < 20:
            y = 20
        if y > 380 - self.sliderh:
            y = 380 - self.sliderh
        self.slider.place(y = y)
        self.move_text(y)

    def move_text(self,y):
        coords = self.get_coords()
        y1 = 360 - self.sliderh
        if y1 != 0:            
            prop = (coords[1]-coords[0])/coords[2]
            prop1 = 1-prop
            prop2 = (y-20)/prop1
            f = prop2/360 * prop1
            self.t.yview(MOVETO,f)

    def release_slider(self,event):
        y = event.y_root - self.y + self.slidery
        if y < 20:
            y = 20
        if y > 380 - self.sliderh:
            y = 380 - self.sliderh
        self.slidery = y
        self.slider.place(y = y)
        self.move_text(y)    
        self.config_slider()

root = Tk()
root.geometry("500x500")
app = App()
root.mainloop()

From what I've read it's really difficult to format scrollbars on Windows. Below is some code for a scrollbar programme which I've written. I'm trying to develop some software which should have these kinds of options, but this seems to me like a lot of code for a widget whose main purpose Tkinter does better. However, the programme should allow you to insert images into the arrows, trough and slider as well as change their colour.

from tkinter import *

class App:
    def __init__(self):
        self.t = Text(root)
        self.t.place(x = 50,y = 50,width = 400,height = 400)
        for i in range(300):
            self.t.insert(END,str(i))
            self.t.insert(END,"\n")
        self.c = Canvas(root)
        self.c.place(x = 450,y = 50,width = 20,height = 400)
        self.trough = Canvas(self.c,bg = "red")
        self.slider = Label(self.c,bg = "grey")
        self.slidery = 20
        self.sliderh = 200
        self.trough.place(x = 0,y = 20,width = 20,height = 360)
        self.slider.place(x = 0,y = 20,width = 20,height = 200)
        self.up = Label(self.c,bg = "purple")
        self.down = Label(self.c,bg = "purple")
        self.up.place(x = 0,y = 0,width = 20,height = 20)
        self.down.place(x = 0,y = 380,width = 20,height = 20)
        self.up.bind("<Button-1>",self.up_view)
        self.down.bind("<Button-1>",self.down_view)
        self.slider.bind("<Button-1>",self.get_slider)
        self.slider.bind("<B1-Motion>",self.move_slider)
        self.slider.bind("<ButtonRelease-1>",self.release_slider)
        self.trough.bind("<Button-1>",self.next_page)
        self.config_slider()

    def next_page(self,event):
        y = event.y
        top = self.slidery - 20
        bottom = self.slidery + self.sliderh - 20
        coords = self.get_coords()
        if y < top:
            self.up_page(coords)
        if y > bottom:
            self.down_page(coords)

    def up_page(self,coords):
        up = -(coords[1] - coords[0])
        self.t.yview(SCROLL,-1,"pages")
        self.pos_slider()

    def down_page(self,coords):
        down = coords[1] - coords[0]
        self.t.yview(SCROLL,1,"pages")
        self.pos_slider()

    def pos_slider(self):
        coords = self.get_coords()
        self.slidery = (coords[0]/coords[2] * 360) + 20
        self.slider.place(y = self.slidery)

    def config_slider(self):
        coords = self.get_coords()
        top = coords[0]
        bottom = coords[1]
        last = coords[2]
        last -= 1
        if bottom-top < last:
            f = int((bottom-top)/last * 360)
        else:
            f = 360
        if f < 10:
            f = 10
        self.sliderh = f
        self.slider.place(height = f,y = self.slidery)

    def get_coords(self):
        root.update()
        top = self.t.index("@0,0")
        bottom = self.t.index("@0,%d" %self.t.winfo_height())
        last = self.t.index(END)
        t = top.split(".")
        b = bottom.split(".")
        l = last.split(".")
        top = int(t[0])
        bottom = int(b[0])
        last = int(l[0])
        return [top,bottom,last]

    def up_view(self,event):
        self.t.yview(SCROLL,-2,"units")
        self.pos_slider()

    def down_view(self,event):
        self.t.yview(SCROLL,2,"units")
        self.pos_slider()

    def get_slider(self,event):
        self.y = event.y_root
        self.config_slider()

    def move_slider(self,event):
        y = event.y_root - self.y
        y += self.slidery
        if y < 20:
            y = 20
        if y > 380 - self.sliderh:
            y = 380 - self.sliderh
        self.slider.place(y = y)
        self.move_text(y)

    def move_text(self,y):
        coords = self.get_coords()
        y1 = 360 - self.sliderh
        if y1 != 0:            
            prop = (coords[1]-coords[0])/coords[2]
            prop1 = 1-prop
            prop2 = (y-20)/prop1
            f = prop2/360 * prop1
            self.t.yview(MOVETO,f)

    def release_slider(self,event):
        y = event.y_root - self.y + self.slidery
        if y < 20:
            y = 20
        if y > 380 - self.sliderh:
            y = 380 - self.sliderh
        self.slidery = y
        self.slider.place(y = y)
        self.move_text(y)    
        self.config_slider()

root = Tk()
root.geometry("500x500")
app = App()
root.mainloop()
暮光沉寂 2024-12-05 08:33:11

不要使用 Box.vbar.config(troughcolor = 'red', bg = 'blue') 更改配置来配置 Box.vbar.configure(troughcolor = 'red', bg = '蓝色')

Instead of using Box.vbar.config(troughcolor = 'red', bg = 'blue') change config to configure Box.vbar.configure(troughcolor = 'red', bg = 'blue')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文