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

BELSEY, Dylan dylan.belsey@baesystems.com
Wed, 16 Oct 2002 14:03:02 +0930


	OK, here goes.... I isolated your class ScrolledList and ran it
independently.  From testing, what I suspect is happening, is that on the
button press ie. downwards action of the mouse button, your function
interrogates the listbox for the currently selected item and gets the one
that "was/is" highlighted.  I believe that the one pointed to only gets set
after this interrogation (perhaps an artefact of the sequence of how
events/bind and widget properties are set), therefore it will pick up the
previous selection.
	The simple fix is to use '<ButtonRelease-1>' instead of '<Button-1>'
for your binding, so that the listbox interrogation happens after the new
value is set.  Even though it looks, visually, like the selection is made on
the downwards press, perhaps the function is run before the visual is
set?!?!? Not sure.
	Hope this makes sense.  Good luck.

		Dylan


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


Scared me there for a moment.... but no, the click works properly - that is,
only on the elements of the scrolled list. And yes, it is most unwise of me
to name variables the way I have done so - so I adjusted the names for
readability.

The functionality that I am looking for is simple. Various elements of the
list are selected for processing - which is why selectmode=EXTENDED. The
buttons determine the specific functionality that should be executed against
the items selected.

The program actually works perfectly - just one click behind. The first time
I select a list element, I get 'selected elements are []' even though I have
selected, for example, the third element of the list (should be 'selected
elements are [2]' (zero justified)); if I select another element - let's say
list element 10, I get 'selected elements are [2]'; the next selection
follows the same pattern ('selected elements are [9]').

Attached is the complete segment of the relevant code (the full code is just
too much to post). Please note that I haven't gotten around to implementing
the buttons yet (even though some code is there). I'm just trying to get the
mouse click logic right before I move on.

class WINMENU(Frame):

 def update_strategy_code(self):

    input1 = "'"+askstring('Enter strategist', 'Strategist Name?')+"'"
    command = 'Select'
    tables = ['strategy_codes']
    columns = ['strategist','code']
    condition = 'strategist='+str(input1)
    sort = 'None'

    sqlcmd = sql.Build_Query (command, tables, columns, condition, sort)

    print sqlcmd

    dbinfo = sql.Query (sqlcmd)

    dis_code = []

    for i in range(nrecs):
      dis_code.append(dbinfo.code[i])

    options = map((lambda i: dis_code[i]), range(nrecs))
    ScrolledList(options)

    Button(self, text='Update',
command=self.edit_strategy_code).pack(side=LEFT)
    Button(self, text='Delete',
command=self.delete_strategy_code).pack(side=RIGHT)

class ScrolledList(Frame):

    def __init__(self, options, parent=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)
        self.makeWidgets(options)

    def handleList(self, event):
        selected_elements=list(self.listbox.curselection())
        print "selected elements are  ", selected_elements

    def makeWidgets(self, options):
        sbar = Scrollbar(self)
        list_contents = Listbox(self, relief=SUNKEN, selectmode=EXTENDED)
        sbar.config(command=list_contents.yview)
        list_contents.config(yscrollcommand=sbar.set)
        sbar.pack(side=RIGHT, fill=Y)
        list_contents.pack(side=LEFT, expand=YES, fill=BOTH)
        pos = 0

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

        list_contents.bind('<Button-1>', self.handleList)
        self.listbox = list_contents
        label=self.listbox.get(ACTIVE)
        print 'info=',label

Thanks.

Regards,

Andy
-----Original Message-----
From: BELSEY, Dylan <dylan.belsey@baesystems.com>
To: tutor@python.org <tutor@python.org>
Date: Tuesday, October 15, 2002 6:52 PM
Subject: RE: [Tutor] Binding the mouse to a list using Tk.


>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
>>
>>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor