변수 pick이 pyglet.clock.schedule_interval을 호출하는 범위에 없어서 이 예제는 실행되지 않아요
어떤 의도인지 잘 모르겠네요. 프로그램을 실행할 때마다 간격이 달라져야 하나요? 간격은 고정하는 편이 더 자연스러워요.
구조를 조금 정리하면 도움이 될 것 같아요.
런타임 동작이 좀 이상하긴 한데, pyglet의 특성일 수도 있고 제가 잘 이해하지 못해서일 수도 있어요. 어쨌든 이 코드는 "Press A"와 "Press B"를 의사 난수 빈도로 번갈아 표시해요.
from random import SystemRandom
from pyglet import window, text, clock, app
# pylint: disable=possibly-used-before-assignment,unused-argument
# note use of variable names in uppercase denoting global scope
TICK_SECONDS = 3.
def refresh(elapsed: float) -> None:
SCREEN.clear()
RNG.choice(CAPTIONS).draw()
if __name__ == "__main__":
SCREEN = window.Window(fullscreen=True)
label_options = { # these parameters are common to both captions
"font_name": "Times New Roman",
"font_size": 72,
"x": SCREEN.width // 2,
"y": SCREEN.height // 2,
"anchor_x": "center",
"anchor_y": "center",
}
# create a tuple of the 2 captions
CAPTIONS = text.Label("Press A", **label_options), text.Label("Press B", **label_options)
RNG = SystemRandom() # only do this once
clock.schedule_interval(refresh, TICK_SECONDS)
app.run()
영어에서 번역·원문 보기This example won't run since the variable pick isn't in the scope containing the invocation of pyglet.clock.schedule_interval
What you intend isn't clear. Should the interval really differ from one execution of this program to another? Keeping the interval constant seems more reasonable.
Reorganizing things could help.
The runtime behavior seems a little strange, although that could simply be something about pyglet or my limited understanding of it. Regardless, this alternates between showing "Press A" and "Press B" at a pseudo-random rate.
from random import SystemRandom
from pyglet import window, text, clock, app
# pylint: disable=possibly-used-before-assignment,unused-argument
# note use of variable names in uppercase denoting global scope
TICK_SECONDS = 3.
def refresh(elapsed: float) -> None:
SCREEN.clear()
RNG.choice(CAPTIONS).draw()
if __name__ == "__main__":
SCREEN = window.Window(fullscreen=True)
label_options = { # these parameters are common to both captions
"font_name": "Times New Roman",
"font_size": 72,
"x": SCREEN.width // 2,
"y": SCREEN.height // 2,
"anchor_x": "center",
"anchor_y": "center",
}
# create a tuple of the 2 captions
CAPTIONS = text.Label("Press A", **label_options), text.Label("Press B", **label_options)
RNG = SystemRandom() # only do this once
clock.schedule_interval(refresh, TICK_SECONDS)
app.run()