更改 Kivy 中的弹出文本
Change popup text in Kivy
我是 Kivy 的新手,所以这可能是一个微不足道的问题。我正在处理一个有两个屏幕的项目,每个屏幕都包含一个生成弹出窗口的按钮。我希望弹出窗口显示包含当前屏幕名称的语句。我的问题是尽管有一种方法可以更改弹出文本,但始终显示占位符文本。为什么 changeText 方法不更改弹出窗口的文本?
我的问题似乎与所示问题类似:
- Kivy Label.text Property doesn't update on the UI
但是我在理解如何将它应用到我的具体情况时遇到了一些问题。
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 代码:
#:import Factory kivy.factory.Factory
<MyManager>:
Screen1:
id: screen1
Screen2:
id: screen2
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
<Screen2>:
name: "two"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen One"
on_release: root.manager.current = "one"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
<PopUp>:
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "PLACEHOLDER TEXT" #this is not the code I want displayed
[1]:
每次您调用 Factory().Popup()
时,它都会创建一个全新的 Popup
,与之前的没有任何关系。你可以做的是:
在 kv:
...
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
p = Factory.PopUp()
p.changeText(root.name)
p.open()
第二个屏幕也是如此。但是每次您释放这些按钮时,它都会创建一个新创建的弹出窗口,浪费了太多内存。你能做的最好的事情是用一个弹出窗口初始化你的屏幕管理器,然后只改变这个弹出窗口的文本:
Python:
...
from kivy.properties import ObjectProperty
...
class PopUp(Popup):
def changeText(self,*args):
self.ids.label.text = "You are on Screen %s!" % args[0].current
class MyManager(ScreenManager):
popup = ObjectProperty()
def __init__(self, **kwargs):
super(MyManager, self).__init__(**kwargs)
self.popup = PopUp()
self.bind(current=self.popup.changeText)
和 kv:
...
<PopUp>:
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "You are on Screen one!"
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
root.manager.popup.open() #Same thing for the second screen
使用 Popup 事件,on_open 更改 Popup 内容,Label 小部件的文本。
Events: on_open:
Fired
when the Popup is opened.
片段
<PopUp>:
on_open:
label.text = "You are on Screen %s!" % app.root.current
id:pop
...
例子
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class MyManager(ScreenManager):
pass
class PopUp(Popup):
pass
class PrimaryApp(App):
def build(self):
return MyManager()
PrimaryApp().run()
primary.kv
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<MyManager>:
Screen1:
id: screen1
Screen2:
id: screen2
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().open()
<Screen2>:
name: "two"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen One"
on_release: root.manager.current = "one"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().open()
<PopUp>:
on_open:
label.text = "You are on Screen %s!" % app.root.current
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "PLACEHOLDER TEXT" #this is not the code I want displayed
输出
我是 Kivy 的新手,所以这可能是一个微不足道的问题。我正在处理一个有两个屏幕的项目,每个屏幕都包含一个生成弹出窗口的按钮。我希望弹出窗口显示包含当前屏幕名称的语句。我的问题是尽管有一种方法可以更改弹出文本,但始终显示占位符文本。为什么 changeText 方法不更改弹出窗口的文本?
我的问题似乎与所示问题类似:
- Kivy Label.text Property doesn't update on the UI
但是我在理解如何将它应用到我的具体情况时遇到了一些问题。
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 代码:
#:import Factory kivy.factory.Factory
<MyManager>:
Screen1:
id: screen1
Screen2:
id: screen2
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
<Screen2>:
name: "two"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen One"
on_release: root.manager.current = "one"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
<PopUp>:
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "PLACEHOLDER TEXT" #this is not the code I want displayed
[1]:
每次您调用 Factory().Popup()
时,它都会创建一个全新的 Popup
,与之前的没有任何关系。你可以做的是:
在 kv:
...
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
p = Factory.PopUp()
p.changeText(root.name)
p.open()
第二个屏幕也是如此。但是每次您释放这些按钮时,它都会创建一个新创建的弹出窗口,浪费了太多内存。你能做的最好的事情是用一个弹出窗口初始化你的屏幕管理器,然后只改变这个弹出窗口的文本:
Python:
...
from kivy.properties import ObjectProperty
...
class PopUp(Popup):
def changeText(self,*args):
self.ids.label.text = "You are on Screen %s!" % args[0].current
class MyManager(ScreenManager):
popup = ObjectProperty()
def __init__(self, **kwargs):
super(MyManager, self).__init__(**kwargs)
self.popup = PopUp()
self.bind(current=self.popup.changeText)
和 kv:
...
<PopUp>:
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "You are on Screen one!"
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
root.manager.popup.open() #Same thing for the second screen
使用 Popup 事件,on_open 更改 Popup 内容,Label 小部件的文本。
Events: on_open:
Fired when the Popup is opened.
片段
<PopUp>:
on_open:
label.text = "You are on Screen %s!" % app.root.current
id:pop
...
例子
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class MyManager(ScreenManager):
pass
class PopUp(Popup):
pass
class PrimaryApp(App):
def build(self):
return MyManager()
PrimaryApp().run()
primary.kv
#:kivy 1.10.0
#:import Factory kivy.factory.Factory
<MyManager>:
Screen1:
id: screen1
Screen2:
id: screen2
<Screen1>:
name: "one"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen Two"
on_release: root.manager.current = "two"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().open()
<Screen2>:
name: "two"
GridLayout:
id: grid
rows: 2
Button:
id: button1
text: "Go to Screen One"
on_release: root.manager.current = "one"
Button:
id: button2
text: "Display Popup"
on_release:
Factory.PopUp().open()
<PopUp>:
on_open:
label.text = "You are on Screen %s!" % app.root.current
id:pop
size_hint: (.5,.5)
title: "Notice!"
Label:
id: label
text: "PLACEHOLDER TEXT" #this is not the code I want displayed