How to prevent Tkinter frame resize?

Discussion in 'Python' started by phil_nospam_schmidt@yahoo.com, Apr 22, 2005.

  1. Guest

    I am trying to prevent a user from resizing a frame beyond its
    "natural" size as given by winfo_reqwidth and winfo_reqheight, without
    any success. Can anyone make any suggestions, based on my code below?

    Thanks!

    from Tkinter import *

    class Table(Frame):
    def __init__(self, master,
    rows=['row 1'], cols=['col 1'],
    row_labels=True,
    col_labels=True,
    row_buttons=True,
    col_buttons=True):
    Frame.__init__(self, master)
    self.rows = rows
    self.cols = cols
    self.row_labels = row_labels
    self.col_labels = col_labels
    self.row_buttons = row_buttons
    self.col_buttons = col_buttons
    self.col_width = 6
    self.draw()
    self.bind('<Configure>', self.changed)
    def changed(self, ev):
    w, h = self.winfo_reqwidth(), self.winfo_reqheight()
    cfg = {}
    if ev.height > h:
    cfg['height'] = h
    if ev.width > w:
    cfg['width'] = w
    if cfg:
    self.config(**cfg) ######## this has no effect ########
    def draw(self):
    if self.row_labels or self.row_buttons:
    col = 1
    for t in self.cols:
    if self.row_labels:
    e = Entry(self, width=self.col_width,
    relief=GROOVE)
    e.insert(INSERT, t)
    e.grid(row=0, column=col+1)
    if self.row_buttons:
    e = Label(self, text=col, width=self.col_width,
    relief=GROOVE,bg='gray', fg='blue')
    e.grid(row=1, column=col+1)
    col += 1
    if self.col_labels or self.col_buttons:
    row = 1
    for t in self.rows:
    if self.col_labels:
    e = Entry(self, width=15,
    relief=GROOVE)
    e.insert(INSERT, t)
    e.grid(row=row+1, column=0)
    if self.col_buttons:
    e = Label(self, text=row, width=self.col_width,
    relief=GROOVE,bg='gray', fg='blue')
    e.grid(row=row+1, column=1)
    row += 1


    if __name__ == '__main__':
    top = Tk()
    cols = ['col %s' % i for i in range(5)]
    rows = ['row %s' % i for i in range(5)]
    s = Table(top, rows=rows, cols=cols)
    s.pack(fill=BOTH, expand=1)
    mainloop()
     
    , Apr 22, 2005
    #1
    1. Advertising

  2. wrote:
    > I am trying to prevent a user from resizing a frame beyond its
    > "natural" size as given by winfo_reqwidth and winfo_reqheight, without
    > any success. Can anyone make any suggestions, based on my code below?
    >
    > Thanks!
    >
    > from Tkinter import *
    >
    > class Table(Frame):
    > def __init__(self, master,
    > rows=['row 1'], cols=['col 1'],
    > row_labels=True,
    > col_labels=True,
    > row_buttons=True,
    > col_buttons=True):
    > Frame.__init__(self, master)
    > self.rows = rows
    > self.cols = cols
    > self.row_labels = row_labels
    > self.col_labels = col_labels
    > self.row_buttons = row_buttons
    > self.col_buttons = col_buttons
    > self.col_width = 6
    > self.draw()
    > self.bind('<Configure>', self.changed)
    > def changed(self, ev):
    > w, h = self.winfo_reqwidth(), self.winfo_reqheight()
    > cfg = {}
    > if ev.height > h:
    > cfg['height'] = h
    > if ev.width > w:
    > cfg['width'] = w
    > if cfg:
    > self.config(**cfg) ######## this has no effect ########


    I'm not sure I follow your code but this method is bound to the
    <Configure> event *but* needs to return the string "break" so that it
    does not pass that event on to the default event handler.



    def changed(self, ev):
    w, h = self.winfo_reqwidth(), self.winfo_reqheight()
    cfg = {}
    if ev.height > h:
    cfg['height'] = h
    if ev.width > w:
    cfg['width'] = w
    if cfg:
    self.config(**cfg) ######## this has no effect ########
    return "break"

    This may do what you want.....

    Cheers
    Martin
     
    Martin Franklin, Apr 22, 2005
    #2
    1. Advertising

  3. Guest

    I've tried this, but it still doesn't work. At this point I suspect
    that the problem is not with the resizing of the frame, but of the
    toplevel widget in which the frame lives. I've been tinkering with
    that, but still without success.
     
    , Apr 22, 2005
    #3
  4. Jeff Epler Guest

    Use the wm_resizable method of the toplevel widget.
    >>> from Tkinter import *; t = Tk(); t.wm_resizable(0,0); t.mainloop()


    Jeff

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

    iD8DBQFCaVNMJd01MZaTXX0RAhxaAKCSR+h6qrlc8SKt3nWUGIHHlk9JrwCffrXw
    HUnG2UnjDDASTq/6g9M1TNI=
    =IVP0
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Apr 22, 2005
    #4
  5. Guest

    That's a useful tidbit of knowledge, but it doesn't quite get at what
    I'm trying to do.

    I want to allow a window to be resized, limited to some maximum size.
    My approach was to use the <Configure> event to capture window resize
    events. Then, if the new size is larger than the maximum, I would force
    it back to the maximum size by reconfiguring the height and width of
    the widget.
     
    , Apr 22, 2005
    #5
  6. Markus Weihs Guest

    Hi!

    I think what you want is maxsize():


    from Tkinter import *

    root = Tk()
    root.maxsize(100,100)
    root.mainloop()


    Regards, Mark
     
    Markus Weihs, Apr 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    10
    Views:
    1,089
    Andrew Thompson
    Dec 12, 2003
  2. wardemon

    Resize Frame

    wardemon, Nov 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    855
    wardemon
    Nov 21, 2006
  3. W. Watson
    Replies:
    6
    Views:
    499
    Eric Brunel
    Feb 18, 2008
  4. TPG
    Replies:
    10
    Views:
    6,236
    Mark Space
    Mar 6, 2008
  5. Replies:
    2
    Views:
    1,282
    Adrienne
    Feb 13, 2005
Loading...

Share This Page