1

I created a custom tkinter widget class which has (among other things) a slider. The below methods run_control_thread and control_loop are methods within said class. They run a thread that sends a command to a motor when a button (also a "child" of my widget class) is pressed. This works fine when only one widget is controlling a motor, but if there are multiple widgets controlling (different) motors, an error pops up when trying to run self.slider.get(), seen in the run_control_thread method. Instead of getting the value of the slider, it gets an exception. I'm quite new to Python, Tkinter, and Stack Overflow, so any help would be appreciated!

    def run_control_thread(self):
        global HTTP, command
        print("running")
        def run_thread():
            global HTTP, command
            current_device = CANDeviceDict[self.current_chosen]
            while(self.control_loop_enabled == True):
                demand = self.slider.get()
                HTTP.send_command(command.control(current_device.model, current_device.id, demand, False, False, 
                                  self.control_mode_value(), 0, 0, False, False))
                time.sleep(0.028)
        control_thread = threading.Thread(target=run_thread)
        control_thread.start()

    def control_loop(self):
        global HTTP, selected_device, command
        if HTTP != None and command != None and selected_device != None:
            if (self.control_loop_enabled):
                self.control_loop_enabled = False
                self.enable_control.config(text="Enable Control")
                self.slider.set(0)
            else:
                self.control_loop_enabled = True
                self.enable_control.config(text="Disable Control")
            self.run_control_thread()

The errors:

>Exception in thread Thread-4"
Traceback (most recent call last):
    File "C:\...\Python\Python38-32\lib\tkinter\__init__.py", line 3457, in get
    return self.tk.getint(value)
    _tkinter.TclError: expected integer but got "expected integer but got "0.00""
    <class 'tkinter.Scale'>

>During handling of the above exception, another exception occurred:
Traceback (most recent call last):
    File "C:\...\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
    File "C:\...\Python\Python38-32\lib\threading.py", line 870, in run
    demand = self.slider.get()
    File "C:\...\Python\Python38-32\lib\tkinter\__init__.py", line 3460, in get
    return self.tk.getdouble(value)
    <class 'tkinter.Scale'>
    _tkinter.TclError: expected floating-point number but got "expected integer but got "0.00""
Aleem L
  • 11
  • 1
  • 1
    Tkinter isn't thread-safe. All access to tkinter widgets needs to be in a single thread. See [this answer](https://stackoverflow.com/questions/38767355/callback-to-python-function-from-tkinter-tcl-crashes-in-windows/38767665#38767665) from one of the developers on the tcl/tk project. – Bryan Oakley Jul 13 '20 at 16:21

0 Answers0