GUI按钮卡住了

时间:2018-05-07 07:04:40

标签: python user-interface tkinter

我使用Tkinter创建了一个GUI。它有三个按钮。一个捕捉图像。第二个用于捕获连续图像,直到我按下第三个按钮,即停止按钮。

当我按下第一个按钮时,它工作正常,即它运行一个捕获图像并将图像保存到特定位置的功能。

但是当我按下第二个按钮以连续捕获图像时,它会开始捕获图像但按钮卡住或GUI停止工作。因为这个我无法阻止这种连续捕获图像,因为我不能按第三个按钮,即停止按钮。

我正在运行一个while循环来连续捕获图像,如果全局变量STOP将具有" 0"我只能打破这个循环。在它中我只能通过按下GUI的停止按钮使其为零。但是我不能按停止按钮来制作这个STOP变量" 0"这样循环就可以破解。我认为while循环正在停止GUI主循环并产生这个问题。如果您有其他选择或解决方案,请分享。this is the code

第二个按钮只需调用此功能

def capture_video():
stop = '1'
l=Lepton()
l.enter()
path="/home/ubuntu/Desktop/IR_videos/vid_"
file=open("/home/ubuntu/Desktop/IR_videos/vid_no.txt",'r')
    no=file.read()
    file.close()
folder_name=path+no
os.mkdir(folder_name)
i=0
print("Image capturing started")
    print("____Press Stop button to stop____")
while True:
    a=l.capture()
    cv2.normalize(a,a,0,65535,cv2.NORM_MINMAX)
    np.right_shift(a,8,a)
    img=np.uint8(a)
    img_name=folder_name+"/"+str(i)+".jpg"
    cv2.imwrite(img_name,img)
    i=i+1
    if stop == "0":
        print("Image capturing stoped\n")
        print("Press video button to capture again")
        break
no=int(no)+1
file=open("/home/ubuntu/Desktop/IR_videos/vid_no.txt",'w')
file.write(str(no))
file.close()
cv2.destroyAllWindows()
l.exit()

这里lepton是一个类,capture()是从flil lepton相机捕获图像的功能

这是GUI停止按钮功能的代码:

def stop_it():
   lep.stop='0'
   time.sleep(1)
   lep.stop='1'

1 个答案:

答案 0 :(得分:0)

你应该在main之外的另一个线程上运行capture_video(),以便main不会冻结。这样,您就可以点击停止按钮。此外,请确保 stop_it 功能可以访问 capture_video 中的 stop 变量。在 capture_video 中, stop 变量是本地变量,因此无法更改。

您可以在这里找到关于如何在另一个线程上运行函数的优秀答案:https://stackoverflow.com/a/14331755/9729313

这是线程和Tkinter的另一个例子: https://stackoverflow.com/a/50175727/9729313