如何用按钮更改循环内的变量? (Tkinter和python)

时间:2018-02-12 15:33:42

标签: python object tkinter tracking

我正在尝试使用用户界面制作对象跟踪示例。我需要几个按钮来改变代码的方式。但是下面的代码没有像我预期的那样工作。布尔变量必须改变,因为当我按下按钮时我可以看到按钮功能分叉,但它不会影响连续工作的循环。如果有人知道我错过了什么你能告诉我吗?谢谢你的帮助。

import PIL
from PIL import Image, ImageTk
import Tkinter as tk
import argparse
import datetime
import cv2
import serial
import time
import os
import sys

#Start_Tracking = tk.BooleanVar
#Start_Tracking.set(False)
p1 = (310,230)
p2 = (330,250)

global Start_Tracking
Start_Tracking = False
tracker = cv2.TrackerKCF_create()

class Application:
    def __init__(self, output_path = "./"):
        self.vs = cv2.VideoCapture(0) 
    self.vs.set(3,640)
    self.vs.set(4,320)
        self.output_path = output_path
        self.current_image = None

        self.root = tk.Tk()  # initialize root window
        self.root.title("Object Tracking GUI")  # set window title
        # self.destructor function gets fired when the window is closed
        self.root.protocol('WM_DELETE_WINDOW', self.destructor)
        self.panel = tk.Label(self.root)  # initialize image panel
        self.panel.pack(padx=10, pady=10)
        self.root.config(cursor="arrow")

        # create a button, that when pressed, tracking will start
        sel_btn = tk.Button(self.root, text='Select A Target', command=self.select_target)
        sel_btn.pack(fill="both", expand=True, padx=10, pady=10)
        # create a button, that when pressed, tracking will end
        rel_btn = tk.Button(self.root, text="Release Selected Target", command=self.release_target)
        rel_btn.pack(fill="both", expand=True, padx=10, pady=10)

        # start a self.video_loop that constantly pools the video sensor
        # for the most recently read frame
        self.video_loop()


    def video_loop(self):
    if not Start_Tracking:
            ok, frame = self.vs.read()  # read frame from video stream
        cv2.rectangle(frame, p1, p2, (0,0,200))
#            frame = cv2.resize(frame, (1500,1000))
            if ok:  # frame captured without any errors
                cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)  # convert colors from BGR to RGBA
                self.current_image = Image.fromarray(cv2image)  # convert image for PIL
                #self.current_image= self.current_image.resize([1280,1024],PIL.Image.ANTIALIAS)
                imgtk = ImageTk.PhotoImage(image=self.current_image)  # convert image for tkinter 
                self.panel.imgtk = imgtk  # anchor imgtk so it does not be deleted by garbage-collector  
                self.panel.config(image=imgtk)  # show the image
                #self.root.attributes("-fullscreen",True)
    else:
            ok, frame = self.vs.read() # do nothing for now

        self.root.after(1, self.video_loop)

    def select_target(self):
        """ Take snapshot and save it to the file """
    Start_Tracking=True
    tracker = cv2.TrackerKCF_create()
    print("Start Pressed")

    def release_target(self):
        """ Take snapshot and save it to the file """
    Start_Tracking=False
    tracker.clear()
    print("Release Pressed")

    def destructor(self):
        """ Destroy the root object and release all resources """
        print("[INFO] closing...")
        self.root.destroy()
        self.vs.release()  # release web camera
        cv2.destroyAllWindows()  # it is not mandatory in this application

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", default="./",
    help="path to output directory to store snapshots (default: current folder")
args = vars(ap.parse_args())

# start the app
print("[INFO] starting...")
pba = Application(args["output"])
pba.root.mainloop()

1 个答案:

答案 0 :(得分:0)

当您在班级中分配Start_Tracking = True时,您将在课程中将本地分配给全球属性,而不是您期望的那样。 See this documentation for more details

要更新全局属性,您必须在赋值之前首先声明它是一个全局变量:

def select_target(self):
        """ Take snapshot and save it to the file """
    global Start_Tracking
    Start_Tracking = True
    tracker = cv2.TrackerKCF_create()
    print("Start Pressed")

def release_target(self):
        """ Take snapshot and save it to the file """
    global Start_Tracking
    Start_Tracking=False
    tracker.clear()
    print("Release Pressed")

然而,我认为让实例方法处理全局属性并不是一个好习惯。

更好的方法是将Start_Tracking作为实例属性,以便在class Application作为self.Start_Tracking的范围内引用它,就像你一样有更多的控制,避免凌乱的命名空间处理。您也可以将其定义为类属性而不是实例属性,但这样会给您similar headache,所以我个人不会这样做。