Tkinter scrolling

F

Felipe Ochoa

So I've been trying to put Frederik Lundh's ScrolledFrame (
http://effbot.org/zone/tkinter-autoscrollbar.htm ) into a class, and
have the frame do more useful things. I tried switching out the
references to root to a frame, and moving it into a class. It runs,
until you try to make the frame/canvas/root smaller than its contents.
From an interactive session, saying something like:
f.botframe["width"] = 200
f.topframe["width"] = 200
f.canvas["width"] = 200

will make the window blink three times, and return to its original
size. Here's the code:

import tkinter as tk

class AutoScrollbar(tk.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()
tk.Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise tk.TclError("cannot use pack with this widget")
def place(self, **kw):
raise tk.TclError( "cannot use place with this widget")

class ScrollFrame:
def __init__(self,master):
self.topframe = tk.Frame(master)
vscrollbar = AutoScrollbar(self.topframe)
vscrollbar.grid(row=0, column=1, sticky='ns')
hscrollbar = AutoScrollbar(self.topframe,
orient=tk.HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky='ew')

self.canvas = tk.Canvas(self.topframe,
yscrollcommand=vscrollbar.set,
xscrollcommand=hscrollbar.set)
self.canvas.grid(row=0, column=0, sticky='news')

vscrollbar.config(command=self.canvas.yview)
hscrollbar.config(command=self.canvas.xview)

# make the canvas expandable
self.topframe.grid_rowconfigure(0, weight=1)
self.topframe.grid_columnconfigure(0, weight=1)

#
# create canvas contents

self.botframe = tk.Frame(self.canvas)
self.canvas.create_window(0, 0, anchor='nw',
window=self.botframe)

self.botframe.update_idletasks()

self.canvas.config(scrollregion=self.canvas.bbox("all"))

Thanks in advance!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top