473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

tkinter puzzler

I have a gui with a bunch of buttons, labels, the usual stuff. It
uses the grid manager:

gui = Frame()
gui.grid()
gui.Label(....).grid() # put some widgets into the gui
... # more widgets

Now at the the very bottom of the gui, I want to add two more buttons,
let's say "stop" and "go". I want "stop" to appear in the gui's lower
left corner and "go" to appear in the gui's lower right corner.
Suppose that up to now, row 16 is the last row in the gui. Then this
works:

Button(gui, text="stop").grid(sticky=W) # starts row 17
Button(gui, text="go").grid(row=17, column=1, sticky=E)

But I don't really want that hardwired row number and I don't want to
keep counting rows and adjusting stuff if I stick new rows in the gui.
So I try the obvious, make one Frame widget containing both new buttons:
stopgo = Frame(gui)
Button(stopgo, "stop").grid(sticky=W)
Button(stopgo, "go").grid(sticky=E)

and try to stretch it across the bottom row of the gui:

stopgo.grid(sticky=E+W)

However, the buttons keep coming out centered in the gui's bottom row
pretty much no matter what I do (I've tried all sorts of combinations).

Am I missing something? I'm not a tkinter whiz and this stuff is
pretty confusing. I did come up with an ugly workaround that I'll
spare you the agony of seeing, but there should be a natural way to do
this.

Thanks for any advice.
Jul 19 '05 #1
5 1635
Paul Rubin wrote:
I have a gui with a bunch of buttons, labels, the usual stuff. It
uses the grid manager:

gui = Frame()
gui.grid()
gui.Label(....).grid() # put some widgets into the gui
... # more widgets

Now at the the very bottom of the gui, I want to add two more buttons,
let's say "stop" and "go". I want "stop" to appear in the gui's lower
left corner and "go" to appear in the gui's lower right corner.
Suppose that up to now, row 16 is the last row in the gui. Then this
works:

Button(gui, text="stop").grid(sticky=W) # starts row 17
Button(gui, text="go").grid(row=17, column=1, sticky=E)

But I don't really want that hardwired row number and I don't want to
keep counting rows and adjusting stuff if I stick new rows in the gui.
So I try the obvious, make one Frame widget containing both new buttons:
stopgo = Frame(gui)
Button(stopgo, "stop").grid(sticky=W)
Button(stopgo, "go").grid(sticky=E)

and try to stretch it across the bottom row of the gui:

stopgo.grid(sticky=E+W)

However, the buttons keep coming out centered in the gui's bottom row
pretty much no matter what I do (I've tried all sorts of combinations).

Am I missing something? I'm not a tkinter whiz and this stuff is
pretty confusing. I did come up with an ugly workaround that I'll
spare you the agony of seeing, but there should be a natural way to do
this.

Thanks for any advice.


Sorry to say Paul but you may have to show us that ugly code! I'm no
whiz with the grid manager (I prefer pack!) but seeing your code will
help us help you

I suspect you need to look at the columnconfigure / rowconfigure methods
of the container (toplevel or frame)
Martin





Jul 19 '05 #2
Paul Rubin wrote:

I think you are missing the columnconfigure()/rowconfigure() methods as
Martin Franklin pointed out. Anyway, here is some code to illustrate the
matter. I've found it helpful to use "false" colors to see what's going on.
the yellow 'main' frame contains the red 'north' and the blue 'south'
frame.

import Tkinter as tk

root = tk.Tk()
main = tk.Frame(root, bg="yellow")
main.pack(expand=True, fill="both")
main.columnconfigure(0, weight=1)
main.rowconfigure(0, weight=1)

north = tk.Frame(main, bg="red")
north.columnconfigure(1, weight=1)
for row, text in enumerate("alpha beta gamma delta".split()):
label = tk.Label(north, text=text)
label.grid(row=row, column=0, sticky="NW")
entry = tk.Entry(north)
entry.grid(row=row, column=1, sticky="NEW")
button = tk.Button(north, text=text.upper())
button.grid(row=row, column=2, sticky="NW")
north.rowconfigure(row, weight=1)
north.grid(column=0, row=0, sticky="NEW")

south = tk.Frame(main, bg="blue")
stop = tk.Button(south, text="Stop")
go = tk.Button(south, text="Go")
stop.grid(column=0, row=0)
go.grid(column=2, row=0)
# the empty column takes all the extra space
south.columnconfigure(1, weight=1)
south.grid(column=0, row=1, sticky="NSEW")

root.mainloop()

Peter

Jul 19 '05 #3
In article <7x***************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
I have a gui with a bunch of buttons, labels, the usual stuff. It
uses the grid manager:

gui = Frame()
gui.grid()
gui.Label(....).grid() # put some widgets into the gui
... # more widgets

Now at the the very bottom of the gui, I want to add two more buttons,
let's say "stop" and "go". I want "stop" to appear in the gui's lower
left corner and "go" to appear in the gui's lower right corner.
Suppose that up to now, row 16 is the last row in the gui. Then this
works:

Button(gui, text="stop").grid(sticky=W) # starts row 17
Button(gui, text="go").grid(row=17, column=1, sticky=E)

But I don't really want that hardwired row number and I don't want to
keep counting rows and adjusting stuff if I stick new rows in the gui.
A couple of options here:
- Put the main portion of the gui into one frame and pack or grid the
button frame below that. That sounds like a natural solution to this
problem based on the way you describe it. (if you do that, I suggest
packing the buttons into their frame; although I usually use the gridder
when in doubt, the packer is often the most natural layout manager for a
row of buttons).

- Increment as you go:
row = 0

wdg.grid(row=row, column=0, ...)
row += 1

wdg2.grid(row=row, column=0, ...)
row += 1

- If you are doing a lot of similar layout, it is worth creating a class
to do your gridding for you. Each instance grids widgets in a particular
frame. It keeps track of the row # for you. For use an existing
gridder, for instance RO.Wdg.Gridder in the RO package
<http://astro.washington.edu/rowen>.

So I try the obvious, make one Frame widget containing both new buttons:
stopgo = Frame(gui)
Button(stopgo, "stop").grid(sticky=W)
Button(stopgo, "go").grid(sticky=E)

and try to stretch it across the bottom row of the gui:

stopgo.grid(sticky=E+W)


This looks OK to me so I'm not sure what's wrong; I think I'd have to
see your actual code. I suggest examining the size of the stopgo frame
by setting its background color.

-- Russell
Jul 19 '05 #4
Martin Franklin <mf********@gatwick.westerngeco.slb.com> writes:
I suspect you need to look at the columnconfigure / rowconfigure methods
of the container (toplevel or frame)


Thanks, columnconfigure turned out to be the answer and Peter Otten's
post showing how to use it was very informative. For some reason
columnconfigure is not documented in the otherwise excellent tkinter
reference manual from New Mexico Tech.

I'm having sort of a different prob now, which is I want to make a
pulldown menu as commonly seen on web pages. The NMT reference
suggests using the MenuButton widget, which sort of works, though the
entrycget and entryconfigure methods that are supposedly on the menu
items aren't really there. The toolkit itself says that MenuButton is
now considered obsolete and Frederik Lundh's manual seems to say
to use Menu somehow instead, but I haven't quite figured out how.

Is there a preferred and normal way to do this? One thing I need is
to be able to disable individual menu choices based on stuff happening
in the application, which is why I wanted to use entryconfigure.

Also, I don't see how to get the little horizontal bar marker on the
button (as seen in IDLE) indicating that the button is a pulldown. A
minute examining the IDLE source code didn't show it either, though I
can probably find it there with more effort.

Thanks as usual.
Jul 19 '05 #5
In article <7x************@ruckus.brouhaha.com>,
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
Martin Franklin <mf********@gatwick.westerngeco.slb.com> writes:
I suspect you need to look at the columnconfigure / rowconfigure methods
of the container (toplevel or frame)


Thanks, columnconfigure turned out to be the answer and Peter Otten's
post showing how to use it was very informative. For some reason
columnconfigure is not documented in the otherwise excellent tkinter
reference manual from New Mexico Tech.

I'm having sort of a different prob now, which is I want to make a
pulldown menu as commonly seen on web pages. The NMT reference
suggests using the MenuButton widget, which sort of works, though the
entrycget and entryconfigure methods that are supposedly on the menu
items aren't really there. The toolkit itself says that MenuButton is
now considered obsolete and Frederik Lundh's manual seems to say
to use Menu somehow instead, but I haven't quite figured out how.


MenuButton is (as far as I know) the recommended way to do this
(and I've been puzzled by F. Lendh's assertion that they are
obsolete). The basics are:

mbut = Tkinter.Menubutton.(master, text="MyMenu", inicatoron=True)
mnu = Tkinter.Menu(mbut, tearoff=False)
mbut["menu"] = mnu

where indicatoron=True gives you the "this is a pop-up menu" indicator
icon. The circular reference is icky but appears to be necessary (i.e.
having the menubutton be a parent to the menu and the menu attribute of
the menubutton point to the menu).

I suggest you buy Grayson's "Python and Tkinter Programming"
(it has a very useful reference section in the back, though
unfortunately it is somewhat incomplete). For more serious work you'll
also want Welch's "Practical Programming in Tcl and Tk" (lots of good
details about Tk).

-- Russell
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
4
by: Tim Daneliuk | last post by:
I am trying to initialize a menu in the following manner: for entry in : func = entry UI.ShortBtn.menu.add_command(label=entry, command=lambda: func(None)) However, at runtime, each of the...
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
3
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.