#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Question Tkinter: How to make LARGE button using grid manager


    (Crossposting Note: I've posted this already in Python Forum (I'm not permitted to reveal the url of this forum, though), but didn't get any response)

    I'm using Python 2.3.5 on OSX 10.4.

    Using the grid manager, I would like to have a button, which spans two rows.

    My first approach was this:



    self.add_button=Button(self.widget,text=self.S.add_button_text(),command=self.add_word).grid(row=0,c olumn=2,rowspan=2)



    The button created, has the same height as a button layed out without 'rowspan'; it is just centered between first and second row.

    Of course I could explicitly set the height to "twice the height of a row in the grid", when creating the Button object, but I don't know in advance, how high a grid row will be.

    Is there some (easy) way to have the button to be created automatically, so that it covers two grid rows?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    If you have an even number of rows you could grid half as many frames as there are rows. Into each frame you could pack accordingly the upper and lower rows. Into the one large frame you could fill with your big button or text. That idea sounds dumb.

    If, after the fact you resized the one cell using your current scheme, would that work? Or would it expand the entire grid?

    I've never deliberately chosen to use the grid manager, I'm just winging it based on your description.

    You could use a Canvas ! The Canvas lets you choose placement.

    Hmm. Maybe you could subclass the Grid manager and somehow patch up your special case. That's what I'd investigate first.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    self.add_button=Button(self.widget,text=self.S.add_button_text(),command=self.add_word).grid(row=0,c olumn=2,rowspan=2)
    will give you a bad object reference.
    Use ...
    Code:
    self.add_button=Button(self.widget,text=self.S.add_button_text(),command=self.add_word)
    self.add_button.grid(row=0, column=2, rowspan=2)
    To make rowspan work you have to give the button the proper height or use a multi-line text ...
    Code:
    ''' tk_grid_span101.py
    give button width and height to span a number of columns or rows
    '''
    
    try:
        # Python2
        import Tkinter as tk
    except ImportError:
        # Python3
        import tkinter as tk
    
    root = tk.Tk()
    
    # width is in text characters
    # height is in text lines
    b1 = tk.Button(root, text="Button1", width=12)
    b2 = tk.Button(root, text="Button2", width=36, bg='green')
    b3 = tk.Button(root, text="Button3", width=24, height=3, bg='red')
    
    # use grid layout manager
    b1.grid(row=0, column=0, sticky='w')
    b2.grid(row=1, column=0, columnspan=3)
    b3.grid(row=2, column=0, rowspan=3, sticky='w')
    
    root.mainloop()
    Last edited by Dietrich; November 4th, 2013 at 02:30 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    492
    Rep Power
    33
    Use sticky="WE" (west to east) to have the button fill both columns. To fill more than one row use rowspan and sticky="NS" or for all use sticky="WENS", but you may have to use something like sticky=W+E+N+S depending on how you import Tkinter. A general reference.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0
    Originally Posted by b49P23TIvg
    If, after the fact you resized the one cell using your current scheme, would that work? Or would it expand the entire grid?
    Thank you for your response.

    In my particular case, resizing is not an operation I care so much, because the entry field in the grid are single line anyway. Maybe I will consider this again, if I change to multi-line entry fields, but for the time being, I will use one of the other solutions suggested here, as they are easy to implement and perfectly suit my current needs.

    Comments on this post

    • b49P23TIvg agrees : I didn't think of sticky. Nor other ideas.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Thumbs up


    Originally Posted by Dietrich
    self.add_button=Button(self.widget,text=self.S.add_button_text(),command=self.add_word).grid(row=0,c olumn=2,rowspan=2)
    will give you a bad object reference.
    Sorry, my mistake. I pasted an earlier version from the clipboard which still had this bug. This are the perils of a clipboard manager with history...

    Originally Posted by Dietrich
    To make rowspan work you have to give the button the proper height
    Thanks a lot, this works!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0
    Originally Posted by dwblas
    use sticky="WENS", but you may have to use something like sticky=W+E+N+S depending on how you import Tkinter.
    Thanks a lot for this suggestion.

    BTW, why does the it depend on the way how to import Tkinter, which syntax ("WENS" or W+E+N+S) I can use?

    Ronald

IMN logo majestic logo threadwatch logo seochat tools logo