3

I have been having trouble keeping the canvas/window from just expanding to the size of the information I am putting in the widget. I want to add scrollbars and limit the size of the canvas. This is for a popup window within a larger program. Here is my code

from Tkinter import *
import os
import tkMessageBox

class ClusterDialog(Toplevel):
    def __init__(self, parent, displayClass, clusterInfo, title = None):        
        Toplevel.__init__(self, parent)
        self.transient(parent)
        #top = self.top = Toplevel(parent)
        if title:
            self.title(title)
        #set parent
        self.parent = parent
        #set class
        self.dClass = displayClass
        #dictionary to store the header data in 
        self.clusterInfo    = clusterInfo        


        #stores checkbox variables
        self.varList = None
        self.boxList = None
        self.name = None

        self.frameTopLevel  = Frame(self,bd=2, width = 200,height=300)
        self.frameTopLevel.pack()

        self.buttonbox(self.frameTopLevel)
        self.frame = Frame(self.frameTopLevel, width = 200,height=300)

        #frame=Frame(root,width=300,height=300)
        self.frame.grid(row=0,column=0)
        self.frame.pack()

        self.canvas=Canvas(self.frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,1000))

        hbar = Scrollbar(self.frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=self.canvas.xview)
        vbar=Scrollbar(self.frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
        vbar.config(command=self.canvas.yview)
        self.canvas.config(width=300,height=300)
        self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
        self.frame.config(height = 100)
        self.body(self.canvas)
        self.canvas.config(width=300,height=300)
        self.grab_set()

Basically I am trying to create a topLevel frame that has a frame within it that contains the scrollbar and the canvas. the self.buttonbox(self.frameTopLevel) adds some buttons and the self.body(self.canvas) adds a bunch of checkboxes for the user to manipulate.

When i run this code my window always expands to the size of the screen and I can't scroll, although the scrollbars are present they do not do anything. Any help would be appreciated? I have been looking at other threads with similar problems but could not find a fix that would work.

Thanks

Onlyjus
  • 5,799
  • 5
  • 30
  • 42

1 Answers1

2

I just got your code to work however I had comment several things out so it did not populate the canvas with buttons. The window did not take up the entire screen though...

Window Size: Maybe try setting the geometry of the toplevel?

self.geometry("300x300+10+10") # numbers corresponding to [width]x[height]+[x offset]+[y offset]

Canvas: The problem is with placing widgets in the canvas. You might want to check this example out: effbot

Onlyjus
  • 5,799
  • 5
  • 30
  • 42
  • Thanks, the self.geometry method worked great for keeping the window to a correct size. However, now I am still not getting the scrollbar to let me scroll down to additional buttons when i populate the canvas. Would you happen to see any additional tricks i could use on this? Thanks – Sugarloafer99 Apr 29 '12 at 01:23
  • I am looking at it... It seems like you are doing everything like in the example over at [effbot](http://effbot.org/zone/tkinter-scrollbar-patterns.htm). – Onlyjus Apr 29 '12 at 16:31
  • So I have been looking around and see that when I call canvas.bbox(ALL) i get None as an output. Does anybody know if this is my problem. I basically have populated the canvas with a bunch of checkbuttons given as the following: var = IntVar() cb = Checkbutton(self.canvas, text = "checkbox", variable = var) cb.grid(row = i, columnspan = 2, sticky=W) – Sugarloafer99 Apr 29 '12 at 16:48
  • @Sugarloafer99: I think the problem might be what you are putting onto the canvas. I put a blue square on the canvas and it works fine: 'self.canvas.create_rectangle(50, 25, 150, 75, fill="blue")' – Onlyjus Apr 29 '12 at 17:28
  • You might want to check this example out: [effbot](http://effbot.org/zone/tkinter-autoscrollbar.htm) – Onlyjus Apr 30 '12 at 02:26
  • @Sugarloafer99: when you pack (or grid) something into the grid, it's not considered part of the canvas. `bbox` only represents window items that were created with `create_window` – Bryan Oakley Apr 30 '12 at 21:46