Python中报t = threading.Thread(target=saySorry) AttributeError: module 'threading' has no attribute 'Thread'
时间: 2023-09-08 16:17:12 浏览: 55
这个错误可能是因为您的Python环境中缺少了线程模块。您可以尝试安装Python的线程模块,或者检查您的Python环境是否已经包含了线程模块。
如果您正在使用Python 2.x版本,您可以使用以下命令安装线程模块:
```python
pip install thread
```
如果您正在使用Python 3.x版本,线程模块已经默认安装,您可以直接使用以下代码:
```python
import threading
t = threading.Thread(target=saySorry)
t.start()
```
如果您的Python环境中仍然无法找到线程模块,您可以尝试重新安装Python来解决这个问题。
相关问题
self.send_thread = threading.Thread(target=asyncio.run, AttributeError: module 'asyncio' has no attribute 'run'
这个错误可能是因为您的 Python 版本太旧,或者您的 asyncio 模块版本不兼容所导致的。asyncio.run() 是从 Python 3.7 开始引入的,如果您使用的是旧版本的 Python,那么您需要使用其他方法来运行 asyncio 协程。
如果您的 Python 版本是 3.7 或更高版本,那么请检查您的 asyncio 模块是否已经安装,并且是否需要升级。您可以尝试在命令行中运行以下命令来升级您的 asyncio:
```
python -m pip install --upgrade asyncio
```
如果您的 Python 版本是 3.6 或更低版本,那么您需要使用其他方法来运行 asyncio 协程。例如,您可以使用 asyncio.get_event_loop().run_until_complete() 方法来运行协程。
另外,您还需要确保您的代码中有一个 asyncio 事件循环在运行。您可以使用以下代码来创建一个事件循环并运行它:
```
loop = asyncio.get_event_loop()
loop.run_until_complete(your_coroutine())
```
请将 "your_coroutine()" 替换为您想要运行的协程函数。
可是t = threading.Thread(target=self.windows_display)使用摄像头会闪烁
闪烁可能是由于您的UI线程和摄像头线程之间的竞争条件造成的。为了解决这个问题,您可以尝试使用`Queue`来传递图像,而不是直接在UI线程和摄像头线程之间共享数据。具体而言,您可以在摄像头线程中将每个图像放入队列中,然后在UI线程中从队列中读取图像并更新画布。
下面是修改后的代码示例:
```
import threading
import cv2
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from queue import Queue
from Background.Thread_Demo import Thread_data
from Data_demo import data_demo
class Camera():
def __init__(self, top):
self.top = top
self.h = int(data_demo.window_height // 1.5)
self.w = int(data_demo.window_width // 1.74)
self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h,
highlightthickness=2, highlightbackground='Black')
self.canvas2.place(relx=0.0, rely=0.032)
self.label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1)
self.label.place(relx=0.32, rely=0.50, anchor='nw')
self.queue = Queue() # 创建一个队列
def Firing_run(self, button_id):
self.bool = True
self.label.destroy()
self.cap = cv2.VideoCapture(0)
t = threading.Thread(target=self.windows_display)
t.start()
button_id.config(text='关闭摄像头', command=lambda: self.Cease_stop(button_id))
def Cease_stop(self, button_id):
self.bool = False
button_id.config(text='打开摄像头', command=lambda: self.Firing_run(button_id))
def windows_display(self):
while self.bool:
ref, frame = self.cap.read()
cv2.waitKey(1)
if ref:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image1 = Image.fromarray(image)
image2 = image1.resize((self.w, self.h), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image=image2)
self.queue.put(photo) # 将图像放入队列中
def update_canvas(self):
if not self.queue.empty():
photo = self.queue.get() # 从队列中获取图像
self.canvas2.create_image(0, 0, image=photo, anchor=tk.NW)
self.canvas2.after(15, self.update_canvas) # 定时更新画布
root = Tk()
camera = Camera(root)
button_id = Button(root, text='打开摄像头', command=lambda: camera.Firing_run(button_id))
button_id.place(relx=0.01, rely=0.01, anchor='nw')
camera.update_canvas() # 启动定时更新画布的函数
root.mainloop()
```
在这个修改后的版本中,我们创建了一个队列,将每个帧的图像放入队列中。然后,在UI线程中,我们使用`update_canvas`函数从队列中获取图像并更新画布。我们使用`after`方法启动定时器,以便每隔一定时间更新画布。这样可以避免UI线程和摄像头线程之间的竞争条件,从而消除闪烁问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)