Python 更改Kivy中的弹出文本
我是新来的基维,所以这可能是一个微不足道的问题。我正在做一个有两个屏幕的项目,每个屏幕都包含一个按钮,可以生成一个弹出窗口。我希望弹出窗口显示包含当前屏幕名称的语句。我的问题是,尽管有更改弹出文本的方法,占位符文本始终显示。为什么changeText方法不更改弹出窗口的文本 我的问题似乎与图中所示类似:Python 更改Kivy中的弹出文本,python,popup,kivy,Python,Popup,Kivy,我是新来的基维,所以这可能是一个微不足道的问题。我正在做一个有两个屏幕的项目,每个屏幕都包含一个按钮,可以生成一个弹出窗口。我希望弹出窗口显示包含当前屏幕名称的语句。我的问题是,尽管有更改弹出文本的方法,占位符文本始终显示。为什么changeText方法不更改弹出窗口的文本 我的问题似乎与图中所示类似: 然而,我在理解如何将其应用于我的具体情况时遇到了一些困难 Python代码: class Screen1(Screen): pass class Screen2(Screen
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
#:导入工厂kivy.Factory.Factory
:
屏幕1:
id:屏幕1
屏幕2:
id:屏幕2
:
姓名:“一”
网格布局:
id:网格
行数:2
按钮:
id:按钮1
文本:“转到屏幕2”
发布时:root.manager.current=“两个”
按钮:
id:按钮2
文本:“显示弹出窗口”
发布时:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
:
姓名:“两个”
网格布局:
id:网格
行数:2
按钮:
id:按钮1
文本:“转到第一屏”
发布时:root.manager.current=“一”
按钮:
id:按钮2
文本:“显示弹出窗口”
发布时:
Factory.PopUp().changeText(root.name)
Factory.PopUp().open()
:
id:流行音乐
尺寸提示:(.5,.5)
标题:“注意!”
标签:
id:标签
文本:“占位符文本”#这不是我想要显示的代码
[1] :每次调用
Factory().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()
和千伏:
...
<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
。。。
:
id:流行音乐
尺寸提示:(.5,.5)
标题:“注意!”
标签:
id:标签
文字:“你在第一屏!”
:
姓名:“一”
网格布局:
id:网格
行数:2
按钮:
id:按钮1
文本:“转到屏幕2”
发布时:root.manager.current=“两个”
按钮:
id:按钮2
文本:“显示弹出窗口”
发布时:
root.manager.popup.open()
使用弹出事件,在打开时更改弹出内容,标记小部件的文本
活动:打开时:解雇 当弹出窗口打开时 片段 一次电压0.kv
#:kivy 1.10.0
#:进口工厂kivy.Factory.Factory
:
屏幕1:
id:屏幕1
屏幕2:
id:屏幕2
:
姓名:“一”
网格布局:
id:网格
行数:2
按钮:
id:按钮1
文本:“转到屏幕2”
发布时:root.manager.current=“两个”
按钮:
id:按钮2
文本:“显示弹出窗口”
发布时:
Factory.PopUp().open()
:
姓名:“两个”
网格布局:
id:网格
行数:2
按钮:
id:按钮1
文本:“转到第一屏”
发布时:root.manager.current=“一”
按钮:
id:按钮2
文本:“显示弹出窗口”
发布时:
Factory.PopUp().open()
:
开放日:
label.text=“您在屏幕%s!”%app.root.current
id:流行音乐
尺寸提示:(.5,.5)
标题:“注意!”
标签:
id:标签
文本:“占位符文本”#这不是我想要显示的代码
输出
我有几个后续问题:1)args[0]的功能是什么?当前?2) self.bind(current=self.popup.changeText)的功能是什么?args[0]是您的屏幕管理器,因此args[0]。current是当前屏幕的名称。绑定函数将changeText方法“绑定”到屏幕管理器当前属性,因此每次当前屏幕更改时,都会自动调用changeText
...
<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:
label.text = "You are on Screen %s!" % app.root.current
id:pop
...
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()
#: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