Этот пример не запустится, поскольку переменная 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()