[Tutor] Button, Button, How do I put the Button

Michael P. Reilly arcege@shore.net
Tue, 20 Feb 2001 08:19:50 -0500 (EST)


> 
> Hello All:
> Below is some code that is meant to hold two buttons and a text window.
> 
> <duh> I don't know quite how to place the buttons</duh>
> 
> Two Questions:
> 1)How do I place the buttons side by side, instead of one above the other?
> 2)Where is documentation relevant to my questions.
> BTW: Thanks to all for all the input on my question on sound()
> Code to follow

You can make two simple changes to get what you want:
> ################################################################
> from Tkinter import *
> from ScrolledText import *
> import sys
> 
> def die(event):
>     sys.exit(0)
> def test(event):
>     pass
> root = Tk()
> text_frame = Frame(root)
> text_frame.pack(expand=1,fill=BOTH)
  text_frame.pack(expand=1,fill=BOTH,side=BOTTOM)

> # build the "quit" button
> q_button = Button(text_frame,width=25)
  q_button = Button(root,      width=25)

> q_button["text"] = "Quit"
> q_button.bind("<Button>",die)
> q_button.pack()
  q_button.pack(side=LEFT)

> #build the "test" button
> t_button = Button(text_frame,width=25)
  t_button = Button(root,      width=25)

> t_button["text"] = "Test"
> t_button.bind("<Button>",test)
> t_button.pack()
  t_button.pack(side=LEFT)

> #would like to put the buttons side by side, but how?
> st = ScrolledText(text_frame ,background="white")
> st.pack()
> 
> root.mainloop()
> ################################################################

The "trick" is the Frame.  Most newbies would probably look at the
Frame class as a useless widget, but its purpose is to organize your
widgets better using the various geometry managers (pack, grid,
place).

Here is an example of your code that will position the buttons above,
below, left or right of the text, depending on what the first program
argument is.

import sys
from Tkinter import *
from ScrolledText import *
root = Tk()
# where do we put the frame?
if sys.argv[1] == 'top':
  button_side=LEFT    # stack buttons left-right or top-bottom within frame
  where_bar = TOP     # buttons above scrolled text
elif sys.argv[1] == 'bottom':
  button_side=LEFT
  where_bar = BOTTOM  # buttons below scrolled text
elif sys.argv[1] == 'left':
  button_side=TOP
  where_bar = LEFT    # buttons left of scrolled text
elif sys.argv[1] == 'right':
  button_side=TOP
  where_bar = RIGHT   # buttons right of scrolled text
# create a frame for just the buttons
button_bar = Frame(root)
q_button = Button(button_bar, text="Quit", command=root.quit)
q_button.pack(side=button_side, expand=YES)
t_button = Button(button_bar, text="Test")
t_button.pack(side=button_side, expand=YES)
st = ScrolledText(root, background='white')
# we don't care where the scrolled text is, so place the buttons first
button_bar.pack(side=where_bar)
st.pack()
root.mainloop()

Notice that I pack the button bar before the ScrolledText, that I
don't create the text_frame widget (using the button_bar instead),
and that I put the buttons in the button_bar, but the scrolled text
widget is _not_ put in the same frame.

I'd suggest getting either a Tcl/Tk book, or John E. Grayson's "Python
and Tkinter programming" book.  There are Tkinter web documents that
have been published here recently and can be found through the
Python.org website.  And you might want to look at the files in the
Demo/tkinter/ directory in the distribution.

  -Arcege

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------