更改Kivy中的弹出文本

2024-09-28 22:43:35 发布

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

我是新手,所以这可能是一个微不足道的问题。我正在做一个有两个屏幕的项目,每个屏幕都包含一个生成弹出窗口的按钮。我希望弹出窗口显示一个包含当前屏幕名称的语句。我的问题是,尽管有一个方法来更改弹出文本,占位符文本总是被显示。为什么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]:


Tags: 方法代码text文本self屏幕ondef
2条回答

每次您调用Factory().Popup()时,它都会创建一个全新的Popup,与前一个无关。你能做的是:

以千伏计:

...
<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:

^{pr2}$

以及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 event,on_open更改弹出内容,标记小部件的文本。在

Popup » API

Events: on_open:
             Fired when the Popup is opened.

片段

<PopUp>:
    on_open:
        label.text = "You are on Screen %s!" % app.root.current
    id:pop
    ...

示例

在主.py在

^{pr2}$

在一次.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

输出

Img01 - Popup at Screen 1Img02 - Popup at Screen 2

相关问题 更多 >