Tkinter scale's command being triggered when grid is called on button

Viewed 266

I am attempting to create a layout using Tkinter for Python3 that involves several buttons and scales. The buttons work fine, but the command that I give to the scale widget is called when I call grid on the scale. Why is this happening and what can I do to stop it?

Here is a simplified version of my code:

import tkinter
import time

WINDOW_HEIGHT = 150
WINDOW_WIDTH = 340

class Player(object):
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.geometry(str(WINDOW_WIDTH) + 'x' + str(WINDOW_HEIGHT))
        self.current_time = tkinter.DoubleVar()

        self.progress_bar = tkinter.Scale(self.window,
                variable = self.current_time,
                command = self.set_current_time,
                orient = tkinter.HORIZONTAL,
                showvalue = 0, resolution=.001)

        self.progress_bar.grid(row=1, column=10)

    def set_current_time(self, time):
        print('setting current time')
        print(time)

    def update(self):
        self.window.update_idletasks()
        self.window.update()

def main():
    media_player = Player()
    while True:
        media_player.update()
        time.sleep(.1)

if __name__ == "__main__":
    main()

The set_current_time function should only be called when the slider is actually clicked and moved, however as soon as grid is executed, set_current_time is called with a time value of 0. How can I place the slider without executing the command? After placement the slider works as expected, but I would like to avoid the initial calling of the set_current_time function.

0 Answers
Related