I need help with autohiding tkinter scrollbars when it is not needed. I have found from effbot.org this code that autohides the scrollbar but only with grid geometry. I am not using grid geometry in my case. Here is my code.
import Tkinter as tk
from Tkinter import *
import tkFont
class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise TclError, "cannot use pack with this widget"
def place(self, **kw):
raise TclError, "cannot use place with this widget"
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
w = 1200
h = 650
x = self.winfo_screenwidth()/2 - w/2
y = self.winfo_screenheight()/2 - h/2
self.geometry("%ix%i+%i+%i" % (w, h, x, y))
self.mainTopFrame = Frame(self, height=75)
self.mainTopFrame.pack(side=TOP, fill=X)
self.canvas = Canvas(self, borderwidth=0, bg='#ffffff')
self.mainBottomFrame = Frame(self.canvas, bg='#000000')
self.yscroll = Scrollbar(self.canvas, orient=VERTICAL, command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.yscroll.set)
self.canvas.pack(side=TOP, fill=BOTH, expand=1)
self.yscroll.pack(side=RIGHT, fill=Y)
self.canvas.create_window((4,4), window=self.mainBottomFrame, anchor=NW)
self.mainBottomFrame.bind("<Configure>", self.onFrameConfigure)
self.menuFrame = Frame(self.mainTopFrame, bg='#545454')
self.menuFrame.pack(side=TOP, fill=BOTH, expand=True)
self.container = Frame(self.mainBottomFrame)
self.container.pack(side=TOP, fill=BOTH, expand=True)
self.frames = {}
for F in (MonitorPage, PlanPage, DataLogPage, HelpPage):
self.frame = F(self.container, self)
self.frames[F] = self.frame
self.frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(MonitorPage)
def show_frame(self, cont):
self.frame = self.frames[cont]
self.frame.tkraise()
def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
# grid_remove is currently missing from Tkinter!
self.tk.call("grid", "remove", self)
else:
self.grid()
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise TclError, "cannot use pack with this widget"
def place(self, **kw):
raise TclError, "cannot use place with this widget"
class MonitorPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.labelFont = tkFont.Font(family="Fixedsys", size=15, weight=tkFont.BOLD)
self.leftFrame0 = Frame(self, bg='#888888')
self.leftFrame0.pack(side=LEFT, fill=BOTH)
self.rightFrame0 = Frame(self, bg='#888888')
self.rightFrame0.pack(side=RIGHT, fill=BOTH)
self.upLftFrame0 = Frame(self.leftFrame0)
self.upLftFrame0.pack(side=TOP, fill=BOTH, padx=10, pady=10)
self.dnLftFrame0 = Frame(self.leftFrame0)
self.dnLftFrame0.pack(side=BOTTOM, fill=BOTH, padx=10, pady=10)
self.upLftLblFrame0 = tk.LabelFrame(self.upLftFrame0)
self.upLftLblFrame0.pack(side=TOP, fill=BOTH, padx=5, pady=5)
self.dnLftLblFrame0 = tk.LabelFrame(self.dnLftFrame0)
self.dnLftLblFrame0.pack(side=BOTTOM, fill=BOTH, padx=5, pady=5)
self.rtLblFrame0 = tk.LabelFrame(self.rightFrame0)
self.rtLblFrame0.pack(side=TOP, fill=BOTH, padx=10, pady=10)
self.label0 = Label(self.rtLblFrame0, height=40, width=70)
self.label0.pack()
self.label1 = Label(self.upLftLblFrame0, height=25, width=115)
self.label1.pack()
self.label2 = Label(self.dnLftLblFrame0, height=10, width=115)
self.label2.pack()
class PlanPage(tk.Frame, MainWindow):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
class DataLogPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
class HelpPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
if __name__ == '__main__':
rungui = MainWindow()
rungui.mainloop()
Therefore, I want to autohide the scrollbar even when I am using pack geometry.I hope I made my question clear. I am very new to python and tkinter.
See Question&Answers more detail:
os