奇维弹出问题。如何在kivy模块上显示弹出窗口?

2024-10-03 06:21:38 发布

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

我正试图让我的python和kivy文件打开一个弹出窗口。它说我的Boxlayout对象没有“open\u popup”属性

以下是我的python代码:

from kivy.app import App
from kivy.properties import BooleanProperty, ListProperty
from kivy.modules import inspector
from kivy.core.window import Window
from kivy.uix.popup import Popup


class CustomPopup(Popup):
    pass


class MPMS(App):
    def build(self):
        inspector.create_inspector(Window, self)

    def show_config_popup(self, popup):
        pass

    def open_popup(self):
        the_popup = CustomPopup()
        the_popup.open()


if __name__ == '__main__':
    app = MPMS()
    app.run()    

这是我的奇维

BoxLayout:
    orientation: 'vertical'
    Label:
        text: 'MPMS'
    BoxLayout:
        orientation: 'horizontal'
        size_hint: (1, 0.25)
        BoxLayout:
            orientation: 'vertical'
            Button:
                id: 'screening_button_mainmenu'
                text: 'Screening'
        BoxLayout:
            orientation: 'vertical'
            Button:
                id: 'configuration_button_mainmenu'
                text: 'Configuration'
                on_press: root.open_popup()

<CustomPopup>:
    size_hint: .5, .5
    auto_dismiss: False
    title: "The Popup"
    BoxLayout:
        orientation: 'horizontal'
        Label:
            text: 'popup has appeared'

我试着在youtube和其他网站上查找视频,但我看不出它对我有什么帮助,因为我无法将其应用于我的情况。请帮帮我。提前谢谢


Tags: textfromimportselfappinspectordefopen
1条回答
网友
1楼 · 发布于 2024-10-03 06:21:38

这是因为您正在执行on_press: root.open_popup(),在这种情况下root是您的BoxLayout(该kv规则的根)。你想要的是

on_press: app.open_popup()

因为open_popup()方法在App

相关问题 更多 >