Question

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

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top