[英]Programmatically close a window made with `racket/gui` (to stop a `timer%`)
Racket programs that use racket/gui
run until all of the windows are closed. 使用
racket/gui
球拍程序在所有窗口关闭之前运行。 This makes it easy to write a program like: 这样可以轻松编写如下程序:
#lang racket/gui
(define window (new frame% [label "Hello"] [width 100] [height 100]))
(send window show #t)
And now the program will continue to run until the window is closed. 现在程序将继续运行,直到窗口关闭。
However, it sometimes makes sense to close a window programmatically, for example, if I want a countdown that will close the window and finish after the countdown is done. 但是,有时以编程方式关闭窗口是有意义的,例如,如果我想倒计时关闭窗口并在倒计时结束后完成。
As far as I can tell though, the only way to 'close' a window is the show
method: 据我所知,“关闭”窗口的唯一方法是
show
方法:
(send window show #f)
This however only stops the window from being displayed, but does not actually close the window. 但是这只会阻止窗口显示,但实际上并不关闭窗口。 Normally, this is good enough and the program exits, like in this example:
通常,这已经足够好并且程序退出,就像在这个例子中一样:
#lang racket/gui
(define window (new frame% [label "hello"]))
(send window show #f)
However, if the program has a timer, it will not exit until the timer finishes. 但是,如果程序有一个计时器,它将不会退出,直到计时器结束。 You can set a callback in the window
on-close
, but this is only called when the window is actually closed, not when its hidden with show
. 您可以在窗口
on-close
设置回调,但仅在窗口实际关闭时调用,而不是在show
时隐藏。 For example, this program will not get stuck: 例如,这个程序不会卡住:
#lang racket/gui
(define window
(new (class frame%
(super-new [label "hello"])
(define timer
(new timer%
[interval 1000]
[notify-callback (λ x (displayln "ding"))]))
(define/augment (on-close)
(send timer stop)))))
(send window show #f)
So, is there a way to either figure out when a window is hidden (via the show
) function or programmatically close the window? 那么,有没有办法找出隐藏窗口(通过
show
)功能或以编程方式关闭窗口? If neither is the case, is overwriting the show
method to stop the timer myself a bad idea? 如果不是这种情况,是否会覆盖
show
方法以自行停止计时器?
Since you are subclassing the frame%
class anyway, you can override the show
method 1 to stop the timer whenever the window is closed. 由于您无论如何都是继承
frame%
class,因此只要窗口关闭,您就可以覆盖show
方法1来停止计时器。 (Do remember to restart it when the window reopens if that is important to you.) (请记住在窗口重新打开时重新启动它,如果这对您很重要。)
(define/override (show show?)
(unless show?
(send timer stop))
(super show show?))
Making your overall class look something like: 让你的整体课程看起来像:
#lang racket/gui
(define window
(new (class frame%
(super-new [label "hello"])
(define timer
(new timer%
[interval 1000]
[notify-callback (λ x (displayln "ding"))]))
(define/augment (on-close)
(send timer stop))
(define/override (show show?)
(unless show?
(send timer stop))
(super show show?)))))
(send window show #f)
Now your program will terminate. 现在你的程序将终止。
1 There is a on-superwindow-show
method, but it does not seem to always run when show
is called. 1有一个
on-superwindow-show
方法,但是在调用show
时它似乎并不总是运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.