パイソンでOpenCVを使用し、画像処理をしています。GUIを使った画像処理を
作ってみたいと考えています。そこで、以下のようなことをしていと考えてます。
・OpenCVで表示した画面をクリック → Tkinter のGUIの画面のラベルオブジェクト
の文字を変更
これをやってみるために、下記のコードを試したところ、コード中央の
print(app.bt["text"],"押してる")
の部分が実行されません。
スレッド間処理で、変数のやりとり、イベントのやりとりを行うためには、
どのようにしたらよいのでしょうか?
import cv2 import numpy as np import copy import tkinter as tk import threading def thread1(): class Camera(): def __init__(self): cv2.namedWindow("img",cv2.WINDOW_NORMAL) cv2.setMouseCallback("img",self.onMouseMove) self.img = cv2.imread("src/Lena.jpg") self.img_copy = self.img self.x_pos = None self.y_pos = None self.font = cv2.FONT_HERSHEY_SIMPLEX def run(self): while(True): cv2.imshow("img",self.img) #if self.x_pos is not None: #cv2.putText(self.img,str(self.img[self.y_pos][self.x_pos]),(300,300),self.font, #0.4,(0,0,0),1) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() def onMouseMove(self,event,x,y,flags,parm): if event == cv2.EVENT_LBUTTONDOWN: self.img = copy.deepcopy(self.img_copy) self.x_pos = x self.y_pos = y #app.lb["text"] = "押されたよー" print("画面押した") print(app.bt["text"],"押してる") obj = Camera() obj.run() def thread2(): class Application(tk.Frame): def __init__(self,master =None): super().__init__(master) master.title("タイトル") master.geometry("350x150") self.pack() self.create_widgets() def create_widgets(self): self.lb = tk.Label() self.lb["text"] = "ラベラー" self.lb.pack(side="top") self.bt = tk.Button() self.bt["text"] = "ボタン" self.bt["command"] = self.print_txtval self.bt.pack(side="bottom") def print_txtval(self): print("ボタンが押された") print(self.bt["text"]) root = tk.Tk() app = Application(master = root) app.mainloop() th1 = threading.Thread(target = thread1) th2 = threading.Thread(target = thread2) th1.start() th2.start()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/23 09:35
2019/04/24 00:21