如何使Jupylet应用程序在Jupyter笔记本中的if语句中工作

2024-05-16 20:05:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在笔记本里使用Jupylet应用程序。如果我在一个单元格内运行app.run(),它工作得很好,但是如果我像我的示例代码中那样从If语句触发运行,它什么也不做。没有错误,但应用程序不会显示。我怎样才能解决这个问题

    import sys
    import os
    sys.path.insert(0, os.path.abspath('./..'))
    from jupylet.label import Label
    from jupylet.app import App
    
    app = App(width=320, height=64)
    hello = Label('hello, world', color='cyan', font_size=32, x=app.width, y=22)
    
    @app.run_me_every(1/24)
    def scroll(ct, dt):
        hello.x -= dt * 48
        if hello.right < 0:
            hello.x = app.width
            
    @app.event
    def render(ct, dt):
        app.window.clear()
        hello.draw() 
    
    c = 3
    if c == 3:
        app.run()

Tags: pathrunfromimportapp应用程序helloos
1条回答
网友
1楼 · 发布于 2024-05-16 20:05:47

Jupylet中,app.run()返回一个Jupyter ipywidgets.Image对象。运行单元格时显示的就是该对象

在调用app.run()后,您可以使用app.canvas访问它,如果您在单元格中对其进行计算,则应显示游戏画布。例如:

In []: app.canvas
Out[]: ...game canvas should display here...

相关问题 更多 >