Python Tkinter 滚动条箭头变灰

Python Tkinter scrollbar arrows greyed out

我的滚动条有点问题。功能上一切正常。滚动条出现,对插入文本小部件和滚动作品做出反应。但是在启动应用程序后,所有滚动条的箭头按钮都是灰色的,直到我调整我的应用程序 window。我的框架在网格设置中有什么基本错误吗?我分为四等份,左上(UL),右上(UR),左下(LL),右下(LR)。

import Tkinter
from Tkinter import *

class findhash_tk(Tkinter.Tk):

    def __init__(self, parent):

        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):

        self.frameUL = Tkinter.LabelFrame(self, text="Upper left")   
        self.frameUL.grid(row=0, column=0, sticky=W+E+N+S, padx=2, pady=2)

        self.frameUR = Tkinter.LabelFrame(self, text="Upper right")   
        self.frameUR.grid(row=0, column=1, sticky=W+E+N+S, padx=2, pady=2)

        self.frameLL = Tkinter.LabelFrame(self, text="Lower left")   
        self.frameLL.grid(row=1, column=0, sticky=W+E+N+S, padx=2, pady=2)

        self.frameLR = Tkinter.LabelFrame(self, text="Lower right")   
        self.frameLR.grid(row=1, column=1, sticky=W+E+N+S, padx=2, pady=2)

        self.scrollbarUR = Tkinter.Scrollbar(self.frameUR, orient='vertical')
        self.scrollbarUR.grid(row=0, column=1, sticky=N+S)     

        self.textBoxUR = Tkinter.Text(self.frameUR)
        self.textBoxUR.config(yscrollcommand=self.scrollbarUR.set)
        self.textBoxUR.grid(row=0, column=0, sticky=W+E+N+S)

        self.scrollbarUR.config(command=self.textBoxUR.yview)

        self.scrollbarLLV = Tkinter.Scrollbar(self.frameLL, orient='vertical')
        self.scrollbarLLV.grid(row=0, column=1, sticky=N+S)

        self.scrollbarLLH = Tkinter.Scrollbar(self.frameLL, orient='horizontal')
        self.scrollbarLLH.grid(row=1, column=0, sticky=W+E)

        self.textBoxLL = Tkinter.Text(self.frameLL)
        self.textBoxLL.config(wrap="none", yscrollcommand=self.scrollbarLLV.set, xscrollcommand=self.scrollbarLLH.set)
        self.textBoxLL.grid(row=0, column=0, sticky=W+E+N+S)

        self.scrollbarLLV.config(command=self.textBoxLL.yview)
        self.scrollbarLLH.config(command=self.textBoxLL.xview)

        self.scrollbarLRV = Tkinter.Scrollbar(self.frameLR, orient='vertical')
        self.scrollbarLRV.grid(row=0, column=1, sticky=N+S)

        self.scrollbarLRH = Tkinter.Scrollbar(self.frameLR, orient='horizontal')
        self.scrollbarLRH.grid(row=1, column=0, sticky=W+E)

        self.textBoxLR = Tkinter.Text(self.frameLR)
        self.textBoxLR.config(wrap="none", yscrollcommand=self.scrollbarLRV.set, xscrollcommand=self.scrollbarLRH.set)
        self.textBoxLR.grid(row=0, column=0, sticky=W+E+N+S)

        self.scrollbarLRV.config(command=self.textBoxLR.yview)
        self.scrollbarLRH.config(command=self.textBoxLR.xview)

        for x in range(50): 
            self.textBoxUR.insert(END, str(x) + "\n")

        for x in range(50): 
            self.textBoxLL.insert(END, str(x) + "\n")

        for x in range(50): 
            self.textBoxLR.insert(END, str(x) + "\n")

if __name__ == "__main__":
    app = findhash_tk(None)
    app.title('Test App')
    app.mainloop()

Initial state of gui

之后贴一个self.update()
self.textBoxUR.grid(row=0, column=0, sticky=W+E+N+S)

奇怪的是再往下将不起作用(一些滚动条会变灰,或者左下角的水平线会变色,但它不应该)。这会为您修复它,但它看起来像是一个错误。

编辑 正如 OP 所提到的,这是不一致的,而且无论如何都很奇怪。一种解决方法,在 initialize() 之后放置:

self.wm_geometry("+4000+4000")
self.after(500,lambda:self.wm_geometry("+10+10"))

这很脏,但是通过将 window 放在屏幕外强制刷新 windows(如果你的屏幕大于 4000 像素哇,但你可以增加)并在半年后第二个把它移回去。这看起来像一个奇怪的问题,并且在 Linux 上没有发生(尽管那里的颜色总是灰色的)。

除了文本之外,我在其他小部件上也遇到过这个错误。我提出的解决方案看起来更好,因为 window 在移出屏幕并再次移回时不会闪烁。下面的解决方案检测 Scrollbar 何时从禁用状态变为活动状态,反之亦然,并销毁并重新创建 Scrollbar。

from tkinter import *

if __name__ == "__main__":
    root = Tk()

    ###

    badframe = Frame(root)
    badframe.grid(row=0, column=0)

    badtext = Text(badframe)
    badtext.grid(row=0, column=0)

    badscrollbar = Scrollbar(badframe, command=badtext.yview)
    badscrollbar.grid(row=0, column=1, sticky=N+S)     

    badtext.config(yscrollcommand=badscrollbar.set)

    def fillbad():
        global badtext
        for i in range(50):
            badtext.insert(END, str(i) + "\n")

    badfillbutton = Button(root, command=fillbad, text="fill")
    badfillbutton.grid(row=1, column=0)

    def clearbad():
        global badtext
        badtext.delete(1.0, END)

    badclearbutton = Button(root, command=clearbad, text="clear")
    badclearbutton.grid(row=2, column=0)

    ###

    def fixyscrollcommand(lo, hi):
        global noyscroll, goodframe, goodscrollbar, goodtext

        start = float(lo)
        end = float(hi)

        # weird Windows bug
        if noyscroll != ((end - start) >= 1.0):
            goodscrollbar.destroy()
            goodscrollbar = Scrollbar(goodframe, command=goodtext.yview)
            goodscrollbar.grid(row=0, column=1, sticky=N+S)

        goodscrollbar.set(lo, hi)

        noyscroll = False
        if (end - start) >= 1.0:
            noyscroll = True

    goodframe = Frame(root)
    goodframe.grid(row=0, column=1)

    goodtext = Text(goodframe, yscrollcommand=fixyscrollcommand)
    goodtext.grid(row=0, column=0)

    goodscrollbar = Scrollbar(goodframe, command=goodtext.yview)
    goodscrollbar.grid(row=0, column=1, sticky=N+S)
    noyscroll = True

    def fillgood():
        global goodtext
        for i in range(50):
            goodtext.insert(END, str(i) + "\n")

    goodbutton = Button(root, command=fillgood, text="fill")
    goodbutton.grid(row=1, column=1)

    def cleargood():
        global goodtext
        goodtext.delete(1.0, END)

    goodclearbutton = Button(root, command=cleargood, text="clear")
    goodclearbutton.grid(row=2, column=1)

    ###

    mainloop()