【问题标题】:How do I update a text entity contained within a window panel in Ursina?如何更新 Ursina 窗口面板中包含的文本实体?
【发布时间】:2021-11-27 21:56:59
【问题描述】:

本质上,我正在尝试制作一个记分牌,在完成一项任务后,它会打开记分牌并显示你的分数。但是,每当我运行实际任务时,它都会显示初始值而不是新变量值。如何在窗口面板中更新此值?

示例伪代码:

from ursina import *

score=0

def challenge():
    score += 2
    wp.enabled=True

app = Ursina()

wp = WindowPanel(content=(Text('text' + str(score))) popup=True, enabled=False)

start = Button(parent=scene, text='start', on_click=challenge)

app.run()

【问题讨论】:

  • 它不会自动更新,因为表达式 Text('text' + str(score)) 只计算一次。在这个简单的用例中,您可以在每次需要时重新创建弹出窗口,而不是尝试更改现有的。

标签: python user-interface ursina


【解决方案1】:

先将文本实体赋给一个变量:

text_entity = Text('text' + str(score))
wp = WindowPanel(content=(text_entity,) popup=True, enabled=False)

# to update the text
text_entity.text = 'new text'

【讨论】:

  • 也可以做 wp.content[0].text = 'new text',但它的可读性有点差,取决于顺序。
猜你喜欢
  • 2023-01-07
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
  • 1970-01-01
相关资源
最近更新 更多