[Tutor] Binding the mouse to a list using Tk.

andy surany mongo57a@comcast.net
Mon, 14 Oct 2002 14:19:33 -0400


This is a multi-part message in MIME format.

--Boundary_(ID_rP+Enm3/pQR2u7Cqxy1VAQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

Hi all!

What I want to do is use a button to capture the position/value of the

selected item in a scrolled list. So the user selects the item in the list

with a single click of the left mouse button and then clicks a button.

Should be easy - I'm just not getting it......



I'm using Tkinter. I Created a class (ScrolledList) for a scrolled list which works fine. Created

another class (aaaaa) which populates the list - and it works fine. Created

a handler under ScrolledList (handleList) which should trap the results of

the bind (makeWidgets). Right now, all I'm trying to do is just print out

the value (just testing..) - but my logic is incorrect (actually, I think

that the logic may be correct - it's just in the wrong place...).

Here is a synopsis of the code. The "...." is non-relevant code which I have removed for simplification.



class ScrolledList(Frame):

    def __init__(self, options, parent=None):

        ........

        self.makeWidgets(options)

    def handleList(self, event):

        index = self.listbox.curselection()

        label = self.listbox.get(index)

        print "label is ", label

    def makeWidgets(self, options):

        ........

        list.bind('<Button-1>', self.handleList) 

Class aaaaa

    def update_strategy_code(self):

        ..........

        ScrolledList(options)

        Button(self, text='Update',

        command=self.edit_strategy_code).pack(side=LEFT)

    def edit_strategy_code(self):

        label=self.listbox.get(ACTIVE)

        print 'info2=',label

 TIA!

Andy (mongo57a@comcast.net)


--Boundary_(ID_rP+Enm3/pQR2u7Cqxy1VAQ)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2><FONT face=Arial size=2>
<P>Hi all!</P>
<P>What I want to do is use a button to capture the position/value of the</P>
<P>selected item in a scrolled list. So the user selects the item in the 
list</P>
<P>with a single click of the left mouse button and then clicks a button.</P>
<P>Should be easy - I'm just not getting it......</P>
<P>&nbsp;</P>
<P>I'm using Tkinter. I Created a class (ScrolledList) for a scrolled list which 
works fine. Created</P>
<P>another class (aaaaa) which populates the list - and it works fine. 
Created</P>
<P>a handler under ScrolledList (handleList) which should trap the results 
of</P>
<P>the bind (makeWidgets). Right now, all I'm trying to do is just print out</P>
<P>the value (just testing..) - but my logic is incorrect (actually, I think</P>
<P>that the logic may be correct - it's just in the wrong place...).</P>
<P>Here is a synopsis of the code. The "...." is non-relevant code which I have 
removed for simplification.</P>
<P>&nbsp;</P>
<P>class ScrolledList(Frame):</P>
<P>&nbsp;&nbsp;&nbsp; def __init__(self, options, parent=None):</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ........</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.makeWidgets(options)</P>
<P>&nbsp;&nbsp;&nbsp; def handleList(self, event):</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; index = self.listbox.curselection()</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; label = self.listbox.get(index)</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "label is ", label</P>
<P>&nbsp;&nbsp;&nbsp; def makeWidgets(self, options):</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ........</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; list.bind('&lt;Button-1&gt;', 
self.handleList) </P>
<P>Class aaaaa</P>
<P>&nbsp;&nbsp;&nbsp; def update_strategy_code(self):</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ..........</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ScrolledList(options)</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Button(self, text='Update',</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
command=self.edit_strategy_code).pack(side=LEFT)</P>
<P>&nbsp;&nbsp;&nbsp; def edit_strategy_code(self):</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; label=self.listbox.get(ACTIVE)</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print 'info2=',label</P>
<P>&nbsp;TIA!</P>
<P>Andy (mongo57a@comcast.net)</P></FONT></FONT></DIV></BODY></HTML>

--Boundary_(ID_rP+Enm3/pQR2u7Cqxy1VAQ)--