我是新手,所以这可能是一个微不足道的问题。我正在做一个有两个屏幕的项目,每个屏幕都包含一个生成弹出窗口的按钮。我希望弹出窗口显示一个包含当前屏幕名称的语句。我的问题是,尽管有一个方法来更改弹出文本,占位符文本总是被显示。为什么changeText方法不改变弹出窗口的文本?在
我的问题似乎与图中的问题类似:
然而,我在理解如何将其应用于我的具体情况时遇到了一些困难。在
Python代码:
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class MyManager(ScreenManager):
pass
class PopUp(Popup):
def changeText(self,nameStr):
self.ids.label.text = "You are on Screen %s!" %nameStr #this is text that I want to display
class PrimaryApp(App):
def build(self):
return MyManager()
PrimaryApp().run()
Kv代码:
^{pr2}$[1]:
每次您调用
Factory().Popup()
时,它都会创建一个全新的Popup
,与前一个无关。你能做的是:以千伏计:
第二个屏幕也是一样。但是每次你释放这些按钮都会产生一个新的弹出窗口,浪费了太多的内存。你能做的最好的事情就是用一个弹出窗口初始化你的屏幕管理器,然后只改变这个弹出窗口的文本:
Python:
^{pr2}$以及kv:
使用Popup event,on_open更改弹出内容,标记小部件的文本。在
Popup » API
片段
示例
在主.py在
^{pr2}$在一次.kv在
输出
相关问题 更多 >
编程相关推荐