ManuMeadow ManuMeadow - 1 month ago 17
Python Question

Python Tkinter scrollbar arrows greyed out

I got a little problem with my scrollbars. Functionally everything's working fine. Scrollbars appear, react on inserting to text widgets and scrolling works. But right after starting the application, arrow buttons of all scrollbars are greyed out until I resize my application window. Is there anything basically wrong about my frames in grid setup? I divided into quarters, Upper Left (UL), Upper Right (UR), Lower Left (LL), Lower Right(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

Answer

Stick a self.update() after:

self.textBoxUR.grid(row=0, column=0, sticky=W+E+N+S)

Oddly enough further down will not work (some scrollbars will be greyed out, or left bottom horizontal will be colored while it shouldn't). This will fix it for you, but it kind of looks like a bug.

EDIT As OP mentions this is not consistent, and weird anyway. A workaround, after initialize() put:

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

This is dirty, but forces the refresh in windows by placing the window out of screen (if your screen is bigger than 4000 pixel wow, but you can increase) and after half a second moves it back. This look like a weird issue, and on Linux doesn't happen (though color is greyish there always).