Python Tkinter:在多个线程中引用滑块值会导致错误
我创建了一个自定义tkinter小部件类,其中包括一个滑块。下面的方法run_control_thread和control_loop是上述类中的方法。它们运行一个线程,当按下按钮(也是我的widget类的“子级”)时,该线程向马达发送命令。当只有一个小部件控制一个电机时,这可以正常工作,但是如果有多个小部件控制(不同)电机,则在尝试运行self.slider.get()时会弹出一个错误,如run\u control\u thread方法中所示。它不获取滑块的值,而是获取一个异常。我对Python、Tkinter和Stack Overflow都是新手,所以如果有任何帮助,我将不胜感激Python Tkinter:在多个线程中引用滑块值会导致错误,python,multithreading,tkinter,Python,Multithreading,Tkinter,我创建了一个自定义tkinter小部件类,其中包括一个滑块。下面的方法run_control_thread和control_loop是上述类中的方法。它们运行一个线程,当按下按钮(也是我的widget类的“子级”)时,该线程向马达发送命令。当只有一个小部件控制一个电机时,这可以正常工作,但是如果有多个小部件控制(不同)电机,则在尝试运行self.slider.get()时会弹出一个错误,如run\u control\u thread方法中所示。它不获取滑块的值,而是获取一个异常。我对Python
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()
错误:
>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""
>线程thread-4中的异常
回溯(最近一次呼叫最后一次):
文件“C:\…\Python\Python38-32\lib\tkinter\\ uu init\ uu.py”,第3457行,在get中
返回self.tk.getint(值)
_tkinter.TclError:应为整数但得到“应为整数但得到“0.00”
>在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\…\Python\Python38-32\lib\threading.py”,第932行,在\u bootstrap\u inner中
self.run()
文件“C:\…\Python\Python38-32\lib\threading.py”,第870行,正在运行
demand=self.slider.get()
get中的文件“C:\…\Python\Python38-32\lib\tkinter\\ uuu init\ uu.py”,第3460行
返回self.tk.getdouble(值)
_tkinter.TclError:应为浮点数,但得到“应为整数,但得到“0.00”
Tkinter不是线程安全的。所有对Tkinter小部件的访问都需要在一个线程中。请参阅tcl/tk项目的一名开发人员。Tkinter不是线程安全的。所有对Tkinter小部件的访问都需要在一个线程中。请参阅tcl/tk项目的一名开发人员。