threading.Thread(target=self._reader)
时间: 2023-08-16 17:10:24 浏览: 70
在 Python 中,threading.Thread 是用于创建和管理线程的类。当创建一个 Thread 实例时,可以指定一个可调用的目标函数作为参数,并将其封装在一个线程对象中。
在你提供的代码中,`self._reader` 是一个可调用的目标函数,它可能是一个方法或函数。`threading.Thread(target=self._reader)` 的作用是创建一个新的线程对象,并将 `self._reader` 设置为该线程的目标函数。当启动该线程后,它会调用 `self._reader` 函数来执行相应的操作。
需要注意的是,这段代码只是创建了一个线程对象,并没有启动线程。要启动线程并执行目标函数,需要调用线程对象的 `start()` 方法。例如,可以使用 `thread.start()` 来启动线程并开始执行 `self._reader` 函数。
相关问题
thread = threading.Thread(target=self._run_thread)
根据提供的引用内容,`threading.Thread(target=self._run_thread)`是创建一个Thread对象的语法,其中`target`参数指定了线程要执行的目标函数,即`self._run_thread`。这个Thread对象可以通过调用`start()`方法来启动线程。在`_run_thread`函数中,可以编写线程的具体逻辑。
举个例子,如果我们想要创建一个线程来打印数字,可以这样写:
```python
import threading
class PrintThread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
print("Thread-{}: {}".format(self.num, self.num))
if __name__ == '__main__':
threads = []
for i in range(5):
thread = PrintThread(i)
threads.append(thread)
thread.start()
```
这个例子中,我们创建了一个名为`PrintThread`的类,继承自`threading.Thread`。在`__init__`方法中,我们传入一个数字`num`,表示这个线程要打印的数字。在`run`方法中,我们使用`print`函数打印出线程的编号和数字。在主程序中,我们创建了5个`PrintThread`对象,并将它们添加到一个列表中。然后,我们遍历这个列表,依次启动每个线程。
可是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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)