在Kivy 1.10.0中自定义弹出屏幕

时间:2017-05-22 23:37:36

标签: python kivy

我目前正在尝试使用Kivy 1.10.0在Python 3.6中创建自定义MessageBox。当用户输入错误的用户名或密码时,我想首先将它用作MessageBox的错误消息。 每当我使用CalcRoot类中的nextScreen函数从CalcPopUp类调用open函数时,我都会收到属性错误。

这是我的代码:

class CalcRoot(BoxLayout):

def __init__(self,**kwargs):
    super(CalcRoot,self).__init__(**kwargs)
    self.calc_popup = CalcPopUp(**kwargs)

def nextScreen(self, next_screen):
    #I have some conditions inside this function which works fine
    CalcPopUp.open(self, "Incorrect Login", True)`

class CalcPopUp(Popup):

popup_message = ObjectProperty()
popup_button = ObjectProperty()

def __init__(self, *args, **kwargs):
    super(CalcPopUp,self).__init__(*args, **kwargs)

def open(self, app_message, with_button=True):

    #if user selected the button attribute as true show button else remove
    if with_button:
        if self.popup_button in self.content.children:
            self.content.remove_widget(self.popup_button)
            # if answer is wrong, display button if not visible
    else:
        if self.popup_button not in self.content.children:
            self.content.add_widget(self.popup_button)

    #display text message
    self.message.text = app_message

    #display pop up
    super(CalcPopUp, self).open()

这是我得到的错误:

AttributeError: 'CalcRoot' object has no attribute 'popup_button'

这是与我的screenpop相关联的kivy文件的内容:

<CalcPopUp>:
size_hint: .8, .4
title: "Message"
title_size: root.height *.05
auto_dismiss: False
separator_color: COLOR("#fcfc02") #yellow
popup_button: popup_button
popup_message: popup_message

BoxLayout:
    orientation: 'horizontal'
    padding: root.width * .02, root.height * .02
    spacing: min(root.height, root.width) * .02
    Label:
        id: popup_message
        text: ""
        halign: 'left'
        font_size: root.height / 10
        center_y: .5
        markup: True
    Button:
        id: popup_button
        text: 'Ok'
        size_hint: 1, None
        height: root.height / 20
        on_release: root.dismiss()

1 个答案:

答案 0 :(得分:0)

这就是我的所作所为:

首先,删除.kv文件中的第7行和第8行。我不确定原帖中是否有缩进错误,但现在看看.kv文件应该如何显示:

<CalcPopUp>:
    size_hint: .8, .4
    title: "Message"
    title_size: root.height *.05
    auto_dismiss: False

    BoxLayout: # this is indented to be inside CalcPopUp
        orientation: 'horizontal'
            ... # etc., no changes other than indentation...

我已经更改了.py文件结构了,看看并告诉我是否有任何我需要解释的内容:

from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.base import runTouchApp
from kivy.base import Builder


class CalcRoot(BoxLayout):
    def __init__(self, **kwargs):
        super(CalcRoot, self).__init__(**kwargs)

    def nextScreen(self, next_screen):
        # I have some conditions inside this function which works fine
        popup = CalcPopUp("Incorrect Login", True)
        popup.open()


class CalcPopUp(Popup):
    popup_message = ObjectProperty()
    popup_button = ObjectProperty()

    def __init__(self, app_message, with_button=True, **kwargs):
        super().__init__(**kwargs)

        # if user selected the button attribute as true show button else remove
        if with_button:
            if self.popup_button in self.content.children:
                self.content.remove_widget(self.popup_button)
            # if answer is wrong, display button if not visible
        else:
            if self.popup_button not in self.content.children:
                self.content.add_widget(self.popup_button)

        # display text message
        self.ids.popup_message.text = app_message


Builder.load_file("calcpopup.kv")
root = CalcRoot()
root.nextScreen(next_screen=None)
runTouchApp(root)