[FAQTS] Python Knowledge Base Update -- August 21st, 2000

Fiona Czuczman fiona at sitegnome.com
Mon Aug 21 22:19:39 EDT 2000


Hi Guys,

The latest entries into http://python.faqts.com

cheers,

Fiona


## New Entries #################################################


-------------------------------------------------------------
In Tkinter, can you connect a scrollbar to 2 objects?
http://www.faqts.com/knowledge-base/view.phtml/aid/5447
-------------------------------------------------------------
Fiona Czuczman
Matthew Dixon Cowles

Problem:

In Tkinter, can you connect a scrollbar to 2 objects (In my case I need
it to be on 2 canvases, or canvii, if you prefer).  The
canvases(canvaii) are next to each other horizontally, and I want them
to scroll down like one canvas.  I would just combine them, but I need
two so that the horizontal scroll will work correctly.

Solution:

Yes, you can. All you need to do is to set the scrollbar's command to
your own function that passes the arguments it's called with to the
xview or yview methods of both canvases. I'll append an example.

Isn't "caravanserai" the plural of "canvas" <wink>.


from Tkinter import *

class mainWin:
  def __init__(self,tkRoot):
    self.tkRoot=tkRoot
    self.createWidgets()
    return None

  def createWidgets(self):
    self.c1=Canvas(self.tkRoot,bg="blue",width="2i",height="2i", \
      scrollregion=(0, 0, "4i", "4i"))
    self.c1.pack(side=LEFT)
    self.c2=Canvas(self.tkRoot,bg="green",width="2i",height="2i", \
     scrollregion=(0, 0, "4i", "4i"))
    self.c2.pack(side=LEFT)
    self.sb=Scrollbar(orient="vertical")
    self.sb.pack(side=LEFT,fill=Y)
    self.sb['command']=self.scrollTwo
    self.c2['yscrollcommand']=self.sb.set

    self.c1.create_rectangle("0.5i", "0.5i", "1i", "1i", fill="black")
    self.c2.create_rectangle("0.5i", "0.5i", "1i", "1i", fill="yellow")

    return None

  def scrollTwo(self,*args):
    print args
    apply(self.c1.yview,args)
    apply(self.c2.yview,args)
    return None

def main():
  tkRoot=Tk()
  mainWin(tkRoot)
  tkRoot.mainloop()
  

if __name__=='__main__':
  main()


-------------------------------------------------------------
Is it possible to implement timeout on read?
http://www.faqts.com/knowledge-base/view.phtml/aid/5448
-------------------------------------------------------------
Fiona Czuczman
Peter Schneider-Kamp, Gerrit Holl

Problem:

Is it possible to implement timeout on read?

eg.

answer = sys.stdin.readline()

but I want it to timeout after 30 seconds.


Solution:

Have a look at the select module. For an example how this can be used 
for a timeout (but for socket.read()) see:

http://www.timo-tasi.org/python/timeoutsocket.py

---------

It's not crossplatform, but on Unix, the signal module is your friend.

You could use such code:

<example>
#!/usr/bin/python

import signal
import sys

TIMEOUT = 10

def f(signum, frame):
    print "Too late!"
    print "Signal handler called with signal number", signum
    print frame, "is a", type(frame), "with some useful information"

signal.signal(signal.SIGALRM, f)
signal.alarm(TIMEOUT)

line = sys.stdin.readline()

print "I read: '%s'" % line
</example>

I don't know any cross-platform solution.

Please consult for details:
http://www.python.org/doc/current/lib/module-signal.html


-------------------------------------------------------------
Does anyone have any swell way to display a tree structure?
http://www.faqts.com/knowledge-base/view.phtml/aid/5450
-------------------------------------------------------------
Fiona Czuczman
Yaniv

this might be helpful:
A small and simple tree control, based only on Tkinter.
http://myweb.magicnet.net/~gcash/tree.html

A tree control based on PMW
http://members.home.net/doughellmann/PmwContribD/TreeNavigator.html

The tree widget included in IDLE 0.5 (the "official" python IDE by 
Guido)
http://www.pytho.org/idle

Download it and run idle.py, then go to the file menu and select 'Path 
Browser' from the menu.


-------------------------------------------------------------
Is there anyway to bind a canvas to key events?
http://www.faqts.com/knowledge-base/view.phtml/aid/5451
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain

from Tkinter import *
root=Tk()
canvas=Canvas(root)
canvas.pack()
def myDullEvent(event):
    print "Hallelujah! (is that how you spell it?)"
canvas.bind('<Key-a>',myDullEvent)
canvas.focus_set()
root.mainloop()

The only thing that is slightly tricky about it is that you need to make
sure the canvas has the focus.


-------------------------------------------------------------
How can I generate long (uniformly distributed) random integers?
http://www.faqts.com/knowledge-base/view.phtml/aid/5452
-------------------------------------------------------------
Fiona Czuczman
Tim Roberts, Pat McCann

Problem:

Does anyone know of an easy way to generate long (uniformely 
distributed) random integers, for example, I'd like to be able to write

generator = whrandom.whrandom()
N = generator.randint(1L,
987349857349878957987598743987587598374985739847589L)

Solution:

It is not hard to write a linear congruential random number generator, 
like that used in most run-time libraries.  Here's one that can 
generate 128-bit values:


class BigRand:
  RandSeed = 11111111111111111111111111111111111L

  def Rand(self):
    hold = self.RandSeed
    self.RandSeed = (self.RandSeed * 134775813L + 1L) % (2L**128L)
    return hold

r = BigRand()
for i in range(20):
  print r.Rand()

And a small tip:

If you need multiple streams of random numbers, get them out of one
number generator, instead of one per stream, to avoid the (VERY small)
chance that the streams will be sequence-shifted versions of each other.

Also, check out the answer, that was originally archived in Hans 
Nowak's snippets collection, from Tim Peters:

LongRan - Long random number generator
http://www.faqts.com/knowledge-base/view.phtml/aid/4406/fid/546


## Edited Entries ##############################################


-------------------------------------------------------------
What's the event name for catching mouse movement without a button pressed?
http://www.faqts.com/knowledge-base/view.phtml/aid/3427
-------------------------------------------------------------
Fiona Czuczman
John Grayson

>From John Grayson's book "Python and Tkinter Programming"

I think Example_6_2.py demostrates this, and the event is also noted on 
page 618.

However, this snippet tracks the motion of the mouse without a button 
down...

from Tkinter import *

root = Tk()

def motion(event):
    print 'Mouse: x=%d, y=%d' % (event.x, event.y)

frame = Frame(root, width=250, height=250)
frame.bind('<Motion>', motion)
frame.pack()

root.mainloop()







More information about the Python-list mailing list