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

BELSEY, Dylan dylan.belsey@baesystems.com
Wed, 16 Oct 2002 08:25:26 +0930


Andy,
	I may just be me but I am still a little confused as to how this
code is supposed to work.  Perhaps if you post the whole code (if not too
large) then the list might be of more assistance. I would make the
recommendation of not using variable names that are the same as built-in
functions e.g. "list" in the makeWidgets() function.
	If "self.listbox.get(ACTIVE)" works for you, why not use it in the
handleList() function as well?  Also, it would appear that you are binding
the whole Listbox to the mouse button 1 press ie. if you click anywhere in
the listbox (even on the border), then won't this kick off your handleList
function...is this what you require?
	Just some thoughts.

		Dylan


-----Original Message-----
From: andy surany [mailto:mongo57a@comcast.net]
Sent: Wednesday, 16 October 2002 03:08
To: BELSEY, Dylan; tutor@python.org
Subject: Re: [Tutor] Binding the mouse to a list using Tk.


Ok, this got me a lot closer! But I'm still slightly off. Thanks for the
example Dylan. I stayed with Tk instead of Pmw based on the amount of code
already developed - and because the functions tended to work the same way.
But I used the "example" logic.

So now I am using:

    label = list(self.listbox.curselection())

and it looks pretty good! Except: label is the previous selection - not the
current. I am always behind by 1 (previous selection is item 2 from the
list, current is item 4, label="2").

Here are the 2 methods. As I indicated, "label" is always one behind the
current position; however, label1 is correct.

   def handleList(self, event):
        label=list(self.listbox.curselection())
        print "label is ", label

    def makeWidgets(self, options):
        sbar = Scrollbar(self)
        list = Listbox(self, relief=SUNKEN, selectmode=EXTENDED)
        sbar.config(command=list.yview)                   # xlink sbar and
list
        list.config(yscrollcommand=sbar.set)              # move one moves
other
        sbar.pack(side=RIGHT, fill=Y)                     # pack first=clip
last
        list.pack(side=LEFT, expand=YES, fill=BOTH)       # list clipped
first
        pos = 0

        for label in options:
          list.insert(pos, label)
          pos = pos+1

        list.bind('<Button-1>', self.handleList)          # set event
handler
        self.listbox = list
        label1=self.listbox.get(ACTIVE)
        print 'info=',label1


-----Original Message-----
From: BELSEY, Dylan <dylan.belsey@baesystems.com>
To: 'andy surany' <mongo57a@comcast.net>; tutor@python.org
<tutor@python.org>
Date: Tuesday, October 15, 2002 12:47 AM
Subject: RE: [Tutor] Binding the mouse to a list using Tk.


>Hi Andy,
> In the interest of code sharing, I have attached a file which I
>believe contains some of the operations that you are looking to perform.
It
>is a cut down version of a class I wrote for the current system I am
working
>on.  In its original form it obtains signals from a data server and
displays
>them ready for selection.  The user then selects signals from the left-hand
>box, presses the appropriate button ('->') and the signal appears in the
>selected signals box, on the right hand side.  This window was to aid the
>user in selecting specific signals for monitoring.  I have kept the
relevant
>functionality in the file.
> I am currently running this under WinNT.  I have also made the file
>self running, so all you have to do is get a DOS prompt in the directory
>where you have saved it and then type "example.py" or "python example.py".
> Two windows will appear.  Just ignore the first one (but don't close
>it until you are finished), for this exercise.  It is the root window,
which
>is important/necessary, but I have chosen not to use it here.
> So anyway, run it and experiment with it and then take a look at the
>code, from which I hope you can get some ideas and help.
> If you have any questions, please feel free to ask.
>
> HTH,
> Dylan
>
>
>PS: By the ways Tutors, if you see any glaring deficiencies, problems, etc.
>in the code pls. feel free to comment.  However, I'm not too interested in
a
>code review at this point in time :)
>
>
>
>
>-----Original Message-----
>From: andy surany [mailto:mongo57a@comcast.net]
>Sent: Tuesday, 15 October 2002 13:50
>To: BELSEY, Dylan; tutor@python.org
>Subject: Re: [Tutor] Binding the mouse to a list using Tk.
>
>
>Thanks Dylan. I am using the listbox methods. And everything works. If I
put
>the list.bind in the same method that calls the scrolled list, I even get a
>response when I move the cursor to list selection
(listbox.cursorselection).
>The problem is that I need the selected list item to be independent of the
>mouse (mouse is depressing a button which gets the original highlighted
list
>item). It's almost like I'm losing context.
>
>I haven't looked at getcurselection - which I will do now. I still think
>that my logic is correct - but I haven't placed it in the correct spot.
>
>Regards,
>
>Andy
>-----Original Message-----
>From: BELSEY, Dylan <dylan.belsey@baesystems.com>
>To: tutor@python.org <tutor@python.org>
>Date: Monday, October 14, 2002 6:59 PM
>Subject: RE: [Tutor] Binding the mouse to a list using Tk.
>
>
>>    Only had a brief look at your problem, but I believe that the Tkinter
>>widgets Listbox and ScrolledListBox may be what you need (and save you
>quite
>>a bit of time).  You can use the Listbox methods from within the
>>ScrolledListbox object as well.  You could then possibly associate a
>>function with your button which gets the currently selected item.
>>getcurselection() returns the text while the curselection() will return
the
>>index.
>>    A lot of this is quite similar to what your code has proposed but
using
>>the built-in widgets and their functions may shed some light on an easier
>>solution.
>>
>>    HTH,
>>        Dylan
>>
>>
>>-----Original Message-----
>>From: andy surany [mailto:mongo57a@comcast.net]
>>Sent: Tuesday, 15 October 2002 04:20
>>To: tutor@python.org
>>Subject: [Tutor] Binding the mouse to a list using Tk.
>>
>>
>>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)
>>
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor@python.org
>>http://mail.python.org/mailman/listinfo/tutor
>
>