【问题标题】:how to stop timer with stop function如何使用停止功能停止计时器
【发布时间】:2023-04-11 03:36:01
【问题描述】:

我的代码有问题,当我按下开始按钮计时器开始工作时 但是停止定时器功能不起作用,如何用停止功能停止定时器? 请帮帮我

这是我的代码:

def _update(self):

    """ Update the label with elapsed time. """
    self._elapsedtime = time.time() - self._start
    self._setTime(self._elapsedtime)
    self._timer = self.after(50, self._update)

def _setTime(self, elap):

    """ Set the time string to Minutes:Seconds:Hundreths """
    hours = int(elap/3600)
    minutes = int((elap - hours*3600)/60)
    seconds = int(elap - hours*3600-minutes*60)                
    self.timestr.set('%02d:%02d:%02d' % (hours,minutes, seconds))




def Start(self):

        """ Start the stopwatch, ignore if running. """
        if not self._running:            
          self._start = time.time() - self._elapsedtime
          self._update()
          self._running = 1

def Run(self):                
    #self.btnstop.config(text="Start",command=self.Run)

    #if self.vs.isOpened() == False:
        #self.vs.open(0)

    """ Get frame from the video stream and show it in Tkinter """
    ok, frame = self.vs.read()  # read frame from video stream
    self.Start()
    if cv2.waitKey() == 27:
        self.Stop()
    if ok:  # frame captured without any errors
         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert colors from BGR to RGBA



    self.current_image = Img.fromarray(frame)  # convert image for PIL
    panel = Label(self.canvas_area)  # initialize image panel
    panel.config(width=500,height=500)
    panel.grid(column=0,row=0)
    imgtk = ImageTk.PhotoImage(image=self.current_image)  # convert image for tkinter
    panel.imgtk = imgtk  # anchor imgtk so it does not be deleted by garbage-collector
    panel.config(image=imgtk)  # show the image

    self.after(30, self.Run)  # call the same function after 30 milliseconds
    #self._running=1    

def Stop(self):

    """ Stop the stopwatch, ignore if stopped. """
    if   self._running:

      self.after_cancel(self._timer)            
      self._elapsedtime = time.time() - self._start    
      self._setTime(self._elapsedtime)
      self._running = 0

【问题讨论】:

  • 什么是self.after?如何启动和停止计时器?你的其余代码在哪里?
  • 你能发布一个最小的可重现示例吗?阅读stackoverflow.com/help/minimal-reproducible-example
  • 我不知道最初调用 Run() 是什么,但我发现一旦发生这种情况就没有什么会阻止它的重复执行——它每次运行时都会调用 Start()(这将(重新)启动秒表)。
  • self.Stop()之后添加一个return
  • 代替0, 1 你可以使用True, False - 它会更易读。

标签: python python-3.x tkinter


【解决方案1】:

我无法测试它,所以我猜 - 你忘记了 & 0xff

if cv2.waitKey() & 0xff == 27:

它永远不会执行self.Stop()

在所有 cv2 的教程中,您应该看到 cv2.waitKey() & 0xff


如果您使用print(cv2.waitKey()),那么当您按下ESC 时,您可能会看到它返回的值大于27


当然也可能是其他问题。我不确定,但 cv2.waitKey() 可能仅在您打开 cv2 的窗口时有效,因为系统将密钥发送到活动窗口,而 cv2.waitKey() 可能仅从使用 cv2.imshow() 创建的窗口中获取密钥

import cv2

img = cv2.imread('image.jpg')
cv2.imshow('win', img)

while True:
    key = cv2.waitKey()
    if key > -1:
        print(key)
    if key & 0xff == 27:
        break

cv2.destroyAllWindows()

如果你使用tkinter来显示窗口,那么你应该使用tkinter的方法来获取密钥——即tk.bind(, self.Stop)

import tkinter as tk

# --- functions ---

def stop(event):
    root.destroy()

# --- main ---

root = tk.Tk()

root.bind('<Escape>', stop)
root.focus()

root.mainloop()

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2022-01-03
    相关资源
    最近更新 更多