当我运行以下代码时,我得到了 GIF 中的初始窗口,单击它会打开一个新窗口,但是新窗口中的形状会闪烁。
我已经尝试在绘制之前清除两个窗口,如代码中所示,但是我不确定是什么导致了闪烁。
import pyglet
from pyglet import shapes
from pyglet.window import mouse
window = pyglet.window.Window(960, 540)
window2 = pyglet.window.Window(960, 540, visible=False)
batch = pyglet.graphics.Batch()
batch2 = pyglet.graphics.Batch()
class window_one():
circle = shapes.Circle(700, 150, 100, color=(50, 225, 30), batch=batch)
square = shapes.Rectangle(200, 200, 200, 200, color=(55, 55, 255), batch=batch)
rectangle = shapes.Rectangle(250, 300, 400, 200, color=(255, 22, 20), batch=batch)
rectangle.opacity = 128
rectangle.rotation = 33
line2 = shapes.Line(150, 150, 444, 111, width=4, color=(200, 20, 20), batch=batch)
class window_two():
line = shapes.Line(100, 100, 100, 200, width=19, batch=batch2)
star = shapes.Star(800, 400, 60, 40, num_spikes=20, color=(255, 255, 0), batch=batch2)
@window.event
def on_draw():
window.clear()
window2.clear()
batch.draw()
@window.event
def on_mouse_press(x,y,button, modifier):
window2.set_visible(True)
batch2.draw()
pyglet.app.run()
我尝试了上面的代码,并期待一个窗口出现在点击和形状显示“实心”。
我在 macbook 上通过 IDLE 运行。
查看示例,您的代码看起来不正确。以下是多个窗口的代码示例:
https://github.com/pyglet/pyglet/blob/master/examples/window/multiple_windows.py
pyglet.window
需要自己的on_draw
和on_mouse_press
事件回调:
@window.event
def on_draw():
window.clear()
batch.draw()
@window2.event
def on_draw():
window2.clear()
batch2.draw()
@window.event
def on_mouse_press(x, y, button, modifier):
window.set_visible(False)
window2.set_visible(True)
@window2.event
def on_mouse_press(x, y, button, modifier):
window.set_visible(True)
window2.set_visible(False)