Tkinter - Resizing a canvas with a window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Airporte

    Tkinter - Resizing a canvas with a window

    I'm trying to get my canvas to resize to fill its frame within a window,
    but I can't figure out how to handle the callback data from the window's
    <Configure> properly. It has very strange behavior - resizing randomly
    or growing by itself, shrinking to 0. The following works passably but
    jumps around at random if you move the window and goes nuts if you add
    the button (watch any wrapping):


    from Tkinter import *

    class testApp2:
    def __init__( self, master ):

    self.ma = master
    self.f = Frame( self.ma )
    self.f.pack()
    self.cv = Canvas(self.f, width=25, height=25, bg='red')
    self.cv.pack()
    #self.b1 = Button( self.f, text='hello', command=None )
    #self.b1.pack(s ide='bottom')

    self.ma.bind('< Configure>', self.resize )


    def resize( self, event ):
    #print '(%d, %d)' % (event.width, event.height)
    self.cv.configu re( width = event.width-4, height = event.height-4 )


    root = Tk()
    app = testApp2(root)
    root.mainloop()

  • Jeff Epler

    #2
    Re: Tkinter - Resizing a canvas with a window

    You should just use 'pack' properly. Namely, the fill= and expand=
    parameters. In this case, you want to pack(fill=BOTH, expand=YES).
    For the button, you may want to use pack(anchor=E) or anchor=W to make
    it stick to one side of the window.

    The additional parameters for the button (both creation and packing)
    give a geometry that is closer to the standard buttons in Windows 95
    / Windows 2000. Use those or not, as you see fit.

    Here's the new program:

    from Tkinter import *

    class testApp2:
    def __init__( self, master ):
    self.ma = master
    self.f = Frame( self.ma )
    self.f.pack(fil l=BOTH, expand=YES)
    self.cv = Canvas(self.f, width=25, height=25, bg='red')
    self.cv.pack(fi ll=BOTH, expand=YES)
    self.b1 = Button( self.f, text='Hello', height=1, width=10,
    padx=0, pady=1)
    self.b1.pack(si de=BOTTOM, anchor=E, padx=4, pady=4)



    root = Tk()
    app = testApp2(root)
    root.mainloop()

    Jeff
    PS thanks for including a full, runnable program in your post!

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.6 (GNU/Linux)

    iD8DBQFC5uHTJd0 1MZaTXX0RAoMyAJ 0aJ5eGeaXM18BC7 CY7/1RQt46fqQCgg02s
    vrD3TQnEeYV7V00 +gwig9E8=
    =dt77
    -----END PGP SIGNATURE-----

    Comment

    • Gordon Airporte

      #3
      Re: Tkinter - Resizing a canvas with a window

      Thanks you very much. I found something interesting though, the canvas's
      width and height properties are not updated when it is resized by its
      packing. Looks like an oversight to me, but I've just demonstrated that
      I don't have a complete grasp of Tk, so... I can use a Configure
      callback to keep track of the values, however.


      from Tkinter import *

      class testApp3:
      def __init__( self, master ):
      self.ma = master
      self.f = Frame( self.ma )
      self.f.pack(fil l=BOTH, expand=YES)
      self.cv = Canvas(self.f, width=125, height=125, bg='red')
      self.cv.pack(fi ll=BOTH, expand=YES)
      self.b1 = Button( self.f, text='Hello', height=1, width=10,
      padx=0, pady=1, \
      command = self.howbig )
      self.b1.pack(si de=BOTTOM, anchor=S, padx=4, pady=4)
      self.cv.bind('< Configure>', self.resize )

      def howbig( self ):
      print self.cv['width'], self.cv['height']
      print self.cvw, self.cvh

      def resize( self, event ):
      print '(%d, %d)' % (event.width, event.height)
      self.cvw, self.cvh = event.width-4, event.height-4

      root = Tk()
      app = testApp3(root)
      root.mainloop()

      Comment

      Working...