[Tutor] I didn't do any input inside my gui interface since I could not fugure out how to do that.

Michael P. Reilly arcege@shore.net
Thu, 22 Mar 2001 07:14:40 -0500 (EST)


> I didn't do any input inside my gui interface since I could not fugure out
> how to do that.
> 
> I can figure out how to do the basic widgets.
> 
> I can not figure out how to use them for input purposes.
> 
> And if I did use them for input purposes, I dont see how you could change
> values whenever you wanted.  For example adjusting hours or minutes or
> seconds on a particular clock up and down whenever you wanted with say scale
> widgets.
> 
> Scale widgets are the worst about this, I can do a button that does a
> callback to show current value of scale but that does not help me at all
> with using a scale widget as a means to set hours or minutes or seconds on a
> clock which should be adjustable whenever one wants it to be with very small
> scales for each value.
> 
> The only way I can see me understanding how to do this right (these input
> widgets, especially the scale one) is to see example code somewhere.
> 
> So what I ended up doing was have the user enter initial values from the
> command console dos-like window, and used the GUI interface to show the
> clocks and some descriptive labels.

There are various input widgets, Entry, Text, Button, Checkbutton,
Scalebutton, Listbox, Scale.

Often times, you will want to bind an event to get the input to envoke
some of your own code.

from Tkinter import *
import Tkinter # to get _cnfmerge
class EntryWithStatus(Entry):
  def __init__(self, master, status, cnf={}, **kws):
    cnf = Tkinter._cnfmerge( (cnf, kws) )
    Entry.__init__(self, master, cnf=cnf)
    self.status_widget = status # to be used when we get the data
    self.bind('<Return>', self.enter)
  def enter(self, event):
    data = self.get()
    self.status_widget['text'] = ('Someone typed in "%s"' % data)

status = Label(None)
status.pack(side=BOTTOM, fill=X, expand=YES)
EntryWithStatus(None, status=status).pack(side=TOP)
mainloop()

Whenever the return key is pressed in the Entry widget, a message is placed
in the Label widget.

If you want to change the hour/minute/second, then I'd suggest looking
into Pmw's Counter widget (Pmw is a library on top of Tkinter).

But the Scale widget "command" is a function that would take at least
one argument which is the number being set.  An example from Grayson's
book is:

def setHeight(canvas, heightStr):
  height = string.atoi(heightStr)
  height = height + 21
  y2 = height - 30
  if y2 < 21:
    y2 = 21
  canvas.coords('poly',
      12,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20)
  canvas.coords('line',
      12,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20)

canvas = Canvas(root, width=50, height=50, bd=0, highlightthickness=0)
canvas.create_polygon(0,0,1,1,2,2,fill='cadetblue', tags='poly')
canvas.create_line(0,0,1,1,2,2,0,0, fill='black', tags='line')

scale = Scale(root, orient=VERTICAL, length=284, from_=0, to=250,
        tickinterval=50, command=lambda h, c=canvas: setHeight(c, h))
scale.grid(row=0, column=0, sticky='NE')
canvas.grid(row=0, column=1, sticky='NWSE')
scale.set(100)

This lets you interactively "grow" the displayed arrow by moving the
scale.

  -Arcege

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