【问题标题】:How to change a variable inside of loop with button ? (Tkinter and python)如何使用按钮更改循环内的变量? (Tkinter 和 python)
【发布时间】:2018-02-12 15:33:42
【问题描述】:

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

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()

【问题讨论】:

标签: python object tkinter tracking


【解决方案1】:

当您在类中分配Start_Tracking = True 时,您是在类中本地 分配它,而不是您所期望的global 属性。 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,这样你就有更多的控制权并避免混乱命名空间处理。您也可以将其定义为 class 属性 而不是 instance 属性,但这会给您一个similar headache,所以我个人不会这样做。

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2014-02-24
    • 2015-06-15
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多