0

If I create a tkinter GUI program with a widget that has a callback function, is there a way to ensure that this callback is not executed until the user actually interacts with the widget?

It appears that when the widget is created (scale in the example below), the callback function is executed before the user even clicks on the scale/slider.

I would like for the message "I got called" not to appear until the user clicks the slider, rather than just by running the program.

I'm using Python 2.7.13 (I need to use 2.7 for certain reasons).

M.W.E.

from Tkinter import *

top = Tk()

def Callback_param11(val):
    print('\n\nI got called\n\n')
    # some commands will go here

p1 = DoubleVar()

p1_slider = Scale(top, variable=p1, from_=-10, to=10, command=Callback_param11)
p1_slider.pack()

top.mainloop()
Jbean
  • 1
  • 1
  • I would generally consider this behavior of the Scale widget to be a feature - it means that whatever is displaying or otherwise using the Scale's value automatically gets synced with the initial value. But if it's a problem for you, you can just create the Scale with no `command=` option, and then add it immediately afterwards - `p1_slider['command'] = Callback_param11`. – jasonharper Feb 02 '20 at 02:29
  • Try not setting `command` option when creating the `Scale` and then add `top.after(200, lambda: p1_slider.config(comand=Callback_param11)` before calling mainloop. _The mentioned behaviour occurs in Python2 but not in Python3_. – acw1668 Feb 02 '20 at 04:37
  • That makes sense. Thank you for the insight. – Jbean Feb 03 '20 at 15:09

0 Answers0