【问题标题】:Kivy: Switching between screens from PopupKivy:从弹出窗口切换屏幕
【发布时间】:2019-03-24 21:52:25
【问题描述】:

我在 kivy 中切换屏幕时遇到问题。 从 MainScreen 有一个按钮可以打开一个弹出窗口。 在弹出窗口中有一个按钮,按下时会显示 DisplayScreen。

这是我的 Python 代码。

#this is MainScreen class
class MainScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def displayConfirmationDialog(self):
        confirmationDialog = self.MainScreenConfirmationDialog()
        confirmationDialog.setMessage()
        confirmationDialog.open()

     #this is the function of the button
    def update(self):
          self.displayConfirmationDialog()

    #this is the popup class
    class MainScreenConfirmationDialog(Popup):


        def setYesButton(self):

            screenManager.add_widget(DisplayScreen())
            self.manager.current = 'display_screen'

#this is the DisplayScreen class
class DisplayScreen(Screen):
    pass

这是我的 kivy 代码

<MainScreen>:
    Button:
        text: 'UPDATE'
        on_press:root.update()

<MainScreenConfirmationDialog>:
    GridLayout:
        rows: 3
        Label:
            id: lbl_confirmation
        Button:
            id: b
            text: 'YES'
            on_press: root.setYesButton()
<DisplayScreen>:
    name: 'display_screen'
    GridLayout:
        rows: 4
        row_force_default: True
        row_default_height: 40
        Label:
            text: 'HELLO'
            id: lbl_display_name

当我运行它时,它显示一个错误

    File 'main.py', line 89, in setYesButton self.manager.current = 'display_screen'
AttributeError: 'MainScreenConfirmationDialog' object has no attribute 'manager'

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    Popup 小部件无权访问屏幕管理器。

    解决方案

    由于您可以访问全局变量screenManager,请将self.manager.current = 'display_screen' 替换为screenManager.current = 'display_screen'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多