Jared Cohen | 19 Aug 2004 17:32
Picon
Favicon

Please help -- Tkinter Scale widget with DoubleVar is acting weird

Hi all. I'm currently using a Tkinter Scale widget which uses a DoubleVar as its control variable. I then use the "trace_variable('w', callback)" method to invoke a callback whenever the DoubleVar changes.

The problem is this: the callback is triggered when the slider moves (as it should), but it's also triggered when I just HOVER the mouse over the slider without clicking! Somehow or other, it must think that the control variable is being changed, even though it isn't. I *think* that this problem is related to the loss of precision that DoubleVars can have, but I'm not really sure. Has anyone else experienced this problem? How can I fix this?

As an aid, here's a quick stub program that illustrates the problem. Try (for example) moving the slider to 0.3, release the mouse button, then move the mouse pointer back and forth over the slider WITHOUT clicking. Each time the pointer passes over the slider, it will print the value.


import sys, Tkinter, types

root = Tkinter.Tk()
var = Tkinter.DoubleVar()

def callback(*args):
    print var.get()

var.trace_variable("w", callback)

slider = Tkinter.Scale(root,
                       from_ = 0.0,
                       to = 1.0,
                       resolution = 0.1,
                       variable = var,
                       orient   = 'horizontal')
slider.grid(sticky='ew')

root.mainloop()
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Fredrik Lundh | 19 Aug 2004 19:01
Gravatar

Re: Please help -- Tkinter Scale widget with DoubleVar is acting weird

Jared Cohen wrote:

> Hi all. I'm currently using a Tkinter Scale widget which uses a
> DoubleVar as its control variable. I then use the "trace_variable('w',
> callback)" method to invoke a callback whenever the DoubleVar changes.
>
> The problem is this: the callback is triggered when the slider moves (as
> it should), but it's also triggered when I just HOVER the mouse over the
> slider without clicking! Somehow or other, it must think that the
> control variable is being changed, even though it isn't. I *think* that
> this problem is related to the loss of precision that DoubleVars can
> have, but I'm not really sure. Has anyone else experienced this problem?
> How can I fix this?

you could use a "command" callback instead of a traced variable:

import sys, Tkinter, types

root = Tkinter.Tk()

def callback(value):
    print float(value)

slider = Tkinter.Scale(root,
                       from_ = 0.0,
                       to = 1.0,
                       resolution = 0.1,
                       command = callback,
                       orient   = 'horizontal')
slider.grid(sticky='ew')

root.mainloop()

(use slider.get() to get the current value from outside the callback,
and slider.set(x) to set it).

</F> 

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Michael Lange | 19 Aug 2004 22:41
Picon
Favicon

Re: Please help -- Tkinter Scale widget with DoubleVar is acting weird

On Thu, 19 Aug 2004 11:32:56 -0400
"Jared Cohen" <Jared.Cohen <at> noaa.gov> wrote:

> Hi all. I'm currently using a Tkinter Scale widget which uses a 
> DoubleVar as its control variable. I then use the "trace_variable('w', 
> callback)" method to invoke a callback whenever the DoubleVar changes.
> 
> The problem is this: the callback is triggered when the slider moves (as 
> it should), but it's also triggered when I just HOVER the mouse over the 
> slider without clicking! Somehow or other, it must think that the 
> control variable is being changed, even though it isn't. I *think* that 
> this problem is related to the loss of precision that DoubleVars can 
> have, but I'm not really sure. Has anyone else experienced this problem? 
> How can I fix this?
> 
> As an aid, here's a quick stub program that illustrates the problem. Try 
> (for example) moving the slider to 0.3, release the mouse button, then 
> move the mouse pointer back and forth over the slider WITHOUT clicking. 
> Each time the pointer passes over the slider, it will print the value.
> 
> 
> import sys, Tkinter, types
> 
> root = Tkinter.Tk()
> var = Tkinter.DoubleVar()
> 
> def callback(*args):
>     print var.get()
> 
> var.trace_variable("w", callback)
> 
> slider = Tkinter.Scale(root,
>                        from_ = 0.0,
>                        to = 1.0,
>                        resolution = 0.1,
>                        variable = var,
>                        orient   = 'horizontal')
> slider.grid(sticky='ew')
> 
> root.mainloop()
> 
Hi Jared,

that's interesting, I never noticed this behavior, probably because I used callbacks where excess calls
didn't matter. To avoid excess calls to your callback() you might consider to store the value of var in
a second variable like this:

var = Tkinter.DoubleVar()
oldvalue = var.get()

def callback(*args):
    global oldvalue
    if var.get() != oldvalue:
        oldvalue = var.get()
        print var.get()

I hope this helped

Michael
Jeff Epler | 20 Aug 2004 14:07
Favicon

Re: [Tkinter-discuss] Please help -- Tkinter Scale widget with DoubleVar is acting weird

I do *not* see this behavior on
    fedora-release-2-4 (Fedora Core 2 Linux)
    python-2.3.3-6
    tcl-8.4.5-7
    tk-8.4.5-6

However, while dragging I can see a set to the same value multiple
times.  I suppose this happens when the mouse movement is too small to
move one "resolution" unit.

You could use a class which acts as a function wrapper to eliminate the
redundant calls to "callback":
    class RedundancyEliminator:
        def __init__(self, var, callback):
            self.var = var
            self.callback = callback
            self.lastval = var.get()

        def __call__(self, *args):
            newval = var.get()
            if newval != self.lastval:
                self.lastval = newval
                return self.callback(*args)
            return None

    def callback(*args):
       print var.get()

    var.trace_variable("w", RedundancyEliminator(var, callback))

Jeff
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Jared Cohen | 20 Aug 2004 16:04
Picon
Favicon

Re: [Tkinter-discuss] Please help -- Tkinter Scale widget with DoubleVar is acting weird

Thanks to both of you for your help. I already do have mechanisms in place that prevent the callback from actually doing anything if the variable has not really been changed. But the problem with that method (and with both of your suggestions) is that even if the callback doesn't do anything, it is still getting invoked when it shouldn't be. I was simply trying to figure out WHY, because I felt that it would be more elegant to completely stop the callback from getting invoked incorrectly, than to allow it to be invoked and then have it return without doing anything. But if that's not feasible, then I'll just have to do it the other way :-)
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Michael Lange | 21 Aug 2004 01:09
Picon

Re: Please help -- Tkinter Scale widget with DoubleVar is acting weird

On Fri, 20 Aug 2004 10:04:30 -0400
"Jared Cohen" <Jared.Cohen <at> noaa.gov> wrote:

> Thanks to both of you for your help. I already do have mechanisms in 
> place that prevent the callback from actually doing anything if the 
> variable has not really been changed. But the problem with that method 
> (and with both of your suggestions) is that even if the callback doesn't 
> do anything, it is still getting invoked when it shouldn't be. I was 
> simply trying to figure out WHY, because I felt that it would be more 
> elegant to completely stop the callback from getting invoked 
> incorrectly, than to allow it to be invoked and then have it return 
> without doing anything. But if that's not feasible, then I'll just have 
> to do it the other way :-)
> 

Maybe you could bind your callback to ButtonRelease- and KeyRelease events
instead of using var.trace_variable() (or the scale's command option), however it
may be a little complicated to get exactly the behavior you want.
BTW, I noticed this behavior, too, on both python2.2.2 / tk8.3.3 and python2.3.3 / tk8.4.5;
unfortunately I don't have a clue, what's the reason for this either.

Michael
Chris Nethery | 22 Aug 2004 08:24
Picon
Favicon

Hiding and unhiding frames and/or widgets in general


First, I recall now that I neglected to thank everyone for their help, concerning my last question.  The last
two months have been extraordinarily busy, although that's a poor excuse for bad manners, so thank you
all, belatedly, for your assistance.

I am back again, with another (possibly silly) question regarding hiding and unhiding windows.  I notice
that toplevel windows have the ability to be "withdrawn" or "deiconified" in order to be hidden from view. 
My widgets are not toplevel widgets, so I'm unclear about whether or not I can use these methods or not. 
Either way, I am trying (unsuccessfully) to accomplish something similar, in that I would like to destroy
or withdraw an entryfield widget if a comboBox is displayed, and visa versa.  Also, if neither an
entryfield or a comboBox is displayed, nothing should be displayed in the same spot.

The following code illustrates, to some degree, what I'm trying to accomplish.  But you'll note that the
code has other problems as well:

import Pmw
from Tkinter import *

root = Tk()
Pmw.initialise(root)

class Stuff:

    def __init__(self, root):
        self.root = root

        self.mainframe = Frame(self.root)
        self.mainframe.pack(fill='both', expand=1, padx=5, pady=5)

        self.choices = [
            '',
            'Select the entryField...',
            'Select the entryField again...',
            'Select the entryField yet again...',
                       ]
        self.choices2 = [
            '',
            'Select the comboBox...',
            'Select the comboBox again...',
            'Select the comboBox yet again...',
                        ]
        self.stuffList = [
            '',
            'item 1',
            'item 2',
            'item 3',
            'item 4',
            'item 5',
            'item 6',
            'item 7',
            'item 8',
            'item 9',
            'item 10',
                         ]

        self.cBox = Pmw.ComboBox(
            self.mainframe,
            listheight=225,
            selectioncommand=self.createEntryField,
            entry_width=28, labelmargin=1, labelpos='w', label_text='Select stuff:', scrolledlist_items=self.choices+self.choices2)
        self.cBox.pack()

        self.eFrame = Frame(self.mainframe, borderwidth=0, width=200, height=22, relief=GROOVE)
        self.eField = Pmw.EntryField(
            self.eFrame,
            labelpos=W,
            label_text='Enter Stuff:',
            labelmargin=2,
            entry_width=12,
            validate = None,
            )
        self.eField.pack()

        self.cFrame = Frame(self.mainframe, borderwidth=0, width=200, height=22, relief=GROOVE)
        self.cBox2 = Pmw.ComboBox(
                self.cFrame,
                listheight=165,
                entry_width=12,
                labelmargin=9,
                labelpos='w',
                label_text='Stuff:',
                scrolledlist_items=self.stuffList
                )
        self.cBox2.pack()

    def createEntryField(self, hello):
        for item in self.choices:
            if self.cBox.component('entryfield').component('entry').get() == item:
                self.cFrame.lower()
                self.eFrame.lift()
                self.eFrame.place(relx=0.7, rely=0.08, anchor=NW)
            else:
                self.eFrame.lower()
                self.cFrame.lift()
                self.cFrame.place(relx=0.7, rely=0.08, anchor=NW)

if __name__=='__main__':
    stuff = Stuff(root)
    root.mainloop()

Any ideas?  Thanking you in advance,

Christopher Nethery
Jeff Epler | 22 Aug 2004 18:41
Favicon

Re: Hiding and unhiding frames and/or widgets in general

If you are using "pack" to manage your widgets, then "pack_forget" will
make the widget disappear.  Similarly, there are "grid_forget" and
"place_forget" methods.

When a widget is packed or unpacked, the size of its containing frame
may change, and that propogates up in the widget hierarchy to the
toplevel.  Having the window change size when a field appears or
disappears is probably going to be jarring to the eye.  You can "fix"
this by turning off "geometry propagation" in the frame that contains
the changing widget.  This is done by the grid_propagate or
pack_propagate method of the *container* object.

from Tkinter import *

def size_for_alternatives(parent, kids):
    w = max([k.winfo_reqwidth() for k in kids])
    h = max([k.winfo_reqheight() for k in kids])
    print ([k.winfo_reqwidth() for k in kids]), w
    print ([k.winfo_reqheight() for k in kids]), h
    parent.pack_propagate(0)
    parent.grid_propagate(0)
    parent.configure(width=w, height=h)

def swap():
    if v.get():
        e.pack_forget()
        mb.pack(anchor="w", side="left")

    else:
        mb.pack_forget()
        e.pack(anchor="w", side="left")
        e.focus()
t = Tk()
v = IntVar(t)
c = Checkbutton(t, command=swap, text="Order from menu?", variable=v)
c.pack(anchor="w")

f1 = Frame(t)
l = Label(f1, text="Your order:")
l.pack(side="left")
f = Frame(f1)
f.pack(side="left")
e = Entry(f, width=18)
mb = Menubutton(f, width=8, text="eggs", indicatoron=1, relief="sunken", anchor="w")
m = Menu(mb, tearoff=0); mb.configure(menu=m)
for s in "eggs bacon sausage spam".split():
    m.add_command(label=s, command=lambda s=s: mb.configure(text=s))
f.after_idle(lambda: size_for_alternatives(f, (e, mb)))

f.pack()
f1.pack()
swap()

b = Button(t, text="Place order", command=t.destroy); b.pack(side="top")

f.mainloop()

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
Fredrik Lundh | 23 Aug 2004 13:50
Gravatar

Re: Please help -- Tkinter Scale widget with DoubleVar is acting weird

Jared Cohen wrote:
> I was simply trying to figure out WHY, because I felt that it would be more
> elegant to completely stop the callback from getting invoked incorrectly, than
> to allow it to be invoked and then have it return without doing anything. But
> if that's not feasible, then I'll just have to do it the other way :-)

Note that I did test my snippet, and it doesn't behave as you describe
on any platform I tested it on.  So it's either a platform issue, or a pro-
blem with the exact version of Tk you're using.  If you really want to
pursue this, you could report the bug to the Tk folks.

</F> 
Jeff Epler | 23 Aug 2004 16:31
Favicon

Re: Please help -- Tkinter Scale widget with DoubleVar is acting weird

I looked into this some more, and was able to reproduce the problem
using
    tcl-8.3.5-88
    redhat-release-9-3
I was then also able to demonstrate it on tcl/tk 8.5.

I used this wish script:
    scale .s -variable v -resolution 0.1 -from 0 -to 1
    trace variable v w changed
    proc changed {args} { global v; puts "v changed.  new value: $v" }
    pack .s

I set the scale value to 0.7.  Now, by moving the cursor *between the
slider and the trough*, I get the repeated message "v changed.  new value:
0.7".  (It's the movement between slider and trough, not just hovering,
that made the redundant sets happen for me)

This behavior doesn't seem to depend on the value of tcl_precision.

It *looks* like the problem is in TkRoundToResolution().  It returns
a slightly different value than the incoming value, so that the test
"scalePtr->value == value" in tkUnixScale:TkpSetScaleValue() fails and
it falls through to a Tcl_SetVar call.  This is low-level stuff inside
of tk.  Like Fredrik suggested, your next step is to go to the tcl/tk
community with your report, if it's important to you.

Jeff
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss <at> python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Gmane