November 4th, 2013, 05:38 AM
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:
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?
November 4th, 2013, 08:40 AM
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] are essential for python code and Makefiles!
November 4th, 2013, 03:00 PM
will give you a bad object reference.
To make rowspan work you have to give the button the proper height or use a multi-line text ...
self.add_button.grid(row=0, column=2, rowspan=2)
give button width and height to span a number of columns or rows
import Tkinter as tk
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')
Last edited by Dietrich; November 4th, 2013 at 03:30 PM.
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
November 4th, 2013, 10:26 PM
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.
November 5th, 2013, 05:42 AM
Thank you for your response.
Originally Posted by b49P23TIvg
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
November 5th, 2013, 05:45 AM
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
Thanks a lot, this works!
Originally Posted by Dietrich
November 5th, 2013, 05:48 AM
Thanks a lot for this suggestion.
Originally Posted by dwblas
BTW, why does the it depend on the way how to import Tkinter, which syntax ("WENS" or W+E+N+S) I can use?