import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Background.Thread_Demo import Thread_data class Two_Layout(): def __init__(self, top): self.top = top self.one = True self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=610, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=28) self.delay = 15 # 更新画面的时间间隔(ms) Thread_data(self.windows_display()) def video(self): return cv2.VideoCapture(0) def windows_display(self): if self.one is True: self.cap = self.video() self.one = False ref, frame = self.cap.read() self.canvas2.delete('all') label = tk.Label(self.canvas2, text='未接入摄像头!', font=("黑体", 25), width=15, height=1) if ref is False: label.place(x=400, y=300, anchor='nw') self.cap = cv2.VideoCapture(0) else: for widget in self.canvas2.winfo_children(): widget.destroy() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((1100, 645), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) self.canvas2.after(self.delay, self.windows_display)他这里后面调用了filedialog.askopenfilename()会冲突为什么已经用了线程了呀
时间: 2024-02-14 16:32:58 浏览: 146
即使使用了线程,GUI 应用程序仍然是单线程的,即所有 GUI 操作都必须在主线程中完成。如果您在另一个线程中调用了 filedialog.askopenfilename(),它会尝试创建一个新的窗口,但由于不在主线程中,因此会出现冲突。因此,您需要使用 tkinter 的 after() 方法在主线程中调用 filedialog.askopenfilename() 方法来避免冲突。
相关问题
import threading import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Background.Thread_Demo import Thread_data from Data_demo import data_demo class Two_Layout(): def __init__(self, top): self.top = top self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=1100, height=610, highlightthickness=2, highlightbackground='Black') self.canvas2.place(x=0, y=28) data_demo.display_id = self.canvas2 Thread_data(self.Thread_()) def Thread_(self): self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display()) t.start() def windows_display(self): ref, frame = self.cap.read() data_demo.display_id.delete('all') label = tk.Label(data_demo.display_id, text='未接入摄像头!', font=("黑体", 25), width=15, height=1) if ref is False: label.place(x=400, y=300, anchor='nw') self.cap = cv2.VideoCapture(0) else: for widget in data_demo.display_id.winfo_children(): widget.destroy() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((1100, 645), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) data_demo.display_id.create_image(0, 0, image=self.photo, anchor=tk.NW) data_demo.display_id.after(15, self.windows_display)为什么还是与filedialog.askopenfilename()起了冲突
你的代码中没有引入 `filedialog`,所以很难确定具体原因。但是,在你的代码中,没有直接调用 `filedialog.askopenfilename()`,所以很可能是其他部分的代码和 `filedialog` 产生了冲突。请检查你的代码并确定是否有其他部分调用了 `filedialog.askopenfilename()`。另外,检查你的代码是否正确导入了 `filedialog`。如果你仍然无法解决问题,请提供更多的细节和代码,以便我能够更好地帮助你。
import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 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.38, rely=0.45, anchor='nw') # 启动摄像头按钮控件 def Firing_Camera(self): self.label.config(text="等待连接....") self.Firing_Camera1() def Firing_Camera1(self): self.cap = cv2.VideoCapture(0) ret, frame = self.cap.read() if ret: self.running = True self.thread = threading.Thread(target=self.update, args=()) self.thread.start() else: messagebox.showinfo('提示', '请检查摄像头是否接入或状态是否正常') def update(self): while self.running: ret, frame = self.cap.read() if ret: # process the frame here # ... # convert the frame to an RGB image image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) # update the tkinter label for widget in self.canvas2.winfo_children(): widget.destroy() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) # wait for a fixed amount of time before capturing the next frame self.canvas2.after(15, self.update)为什么摄像头不动
可能是以下原因导致摄像头不动:
1. 摄像头没有接好或者连接错误,需要检查摄像头连接是否正确。可以尝试更换USB接口或者更换摄像头试试。
2. 摄像头的驱动程序没有安装或者不兼容,需要安装或更新摄像头驱动程序。
3. 摄像头被其他程序占用,需要关闭其他占用摄像头的程序,或者重启电脑后再尝试启动摄像头。
4. 摄像头硬件故障,需要更换摄像头或者修理摄像头。
阅读全文