【问题标题】:Calling a method from on_release event of a button in a pop up从弹出窗口中按钮的 on_release 事件调用方法
【发布时间】:2017-09-24 18:58:16
【问题描述】:

正如我在标题中提到的,我正在创建一个带有视频和两个按钮的弹出窗口 - 现在我想为这些按钮实现功能 - 所以,我创建了一个方法来在从弹出窗口中按下按钮时调用我已经创建了 - 但这总是返回 TypeError: addme() 只需要 1 个参数(给定 2 个) - 如果我将此方法设为静态,则它返回 AttributeError: 'Button' object has no attribute 'on_text'

from __future__ import print_function
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.progressbar import ProgressBar
from kivy.uix.video import Video
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import WidgetException
from functools import partial


kv = """
<MainPage>:
    manager:screen_mgr
    do_default_tab: False
    textinputtext: waybill.text
    ScreenManager:
        id: screen_mgr
        #transition: FadeTransition
        Screen:
            manager: screen_mgr
            id: intro
            orientation: 'horizontal'
            name: 'User'
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size
            AnchorLayout:
                anchor_x: 'center'
                anchor_y: 'center'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 20
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'left'
                        size_hint_x: .5
                        TextInput:
                            id: waybill
                            width: 20
                            text: root.textinputtext
                            multiline: False
                            height: self.minimum_height
                            size_hint_y: None
                            font_size: 30
                            focus: True
                            on_text: self.text
                            on_text_validate:app.on_waybill()
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: "Compare"
                            on_release: root.compareClicked()
            AnchorLayout:
                anchor_x: 'right'
                anchor_y: 'bottom'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            id: statebtn
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: "State"
            AnchorLayout:
                anchor_x: 'left'
                anchor_y: 'top'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.5, 'center_y': .5}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            on_release:root.current = root.switch_screen()
                            text: "Admin"
    TabbedPanelHeader:
        text: intro.name
        # store a screen name to link the tab to a screen
        screen: intro.name
"""

Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)

class MainPage(TabbedPanel):

    screen_mgr = ObjectProperty(None)
    textinputtext = StringProperty()

    def __init__(self, **kwargs):
        super(MainPage, self).__init__(**kwargs)
        self.register_event_type('on_text')
        self.textinputtext = "Enter Waybill No."

    def on_text(self):
        return self.textinputtext

    def on_text_validate(self):
        return self.textinputtext

    def on_focus(self, obj, focused):
        if not focused:
            self.dispatch('on_text_validate')

    def compareClicked(self):
        self.progress_bar = ProgressBar()
        self.progress_bar.value = 1
        print (self.on_text())
        if self.on_text()!='Enter Waybill No.' :
            self.popup_1()

    def popup_1(self):
        self.box = BoxLayout(orientation='vertical', padding=(10))
        popup = Popup(title='Warning: Please Place', title_size=(30),
                      title_align='center', content=self.box,
                      size_hint=(None, None), size=(400, 400),
                      auto_dismiss=False)
        self.box.add_widget(Button(text="YES TO COMPARE", on_release=partial(self.addme)))
        self.box.add_widget(Button(text="NO TO GO BACK", on_release=popup.dismiss))
        popup.bind()
        popup.open()

    #@staticmethod
    def addme(self):
        print (self.on_text())

    def switch_screen(self):
        self.manager.transition = FadeTransition()
        self.manager.current = self.manager.previous()
        return self.manager.current

    def switch_to(self, header):
        self.manager.current = header.screen
        self.current_tab.state = "normal"
        header.state = 'down'
        self._current_tab = header

class KartScanApp(App):
    def build(self):
        return MainPage()

    def on_waybill(self):
        waybill.bind(text=MainPage.on_text)
        return waybill.text

if __name__ == '__main__':
    KartScanApp().run()

请帮我解决这个问题,我在这里缺少一些基础知识

【问题讨论】:

  • 你能把你的例子减少到尽可能少的代码行吗?理想情况下,除了 kivy 之外,也没有依赖关系。乍一看,on_text 看起来很奇怪,也不需要注册为已经注册的。如果您减少代码,我会更深入地研究。
  • 嗨@palimpalim,我已经删除了外部依赖项并删除了一些不必要的方法,你能看看它并建议我有什么问题吗?
  • 你不能举一个只有几行并且有同样问题的例子吗?

标签: python python-2.7 kivy kivy-language


【解决方案1】:

我知道我的代码有什么问题,每当我从弹出窗口中按钮的 on_release 事件调用任何方法时,该方法总是将按钮保存为对象 - 所以当你创建一个方法时接受还有一个参数可以保存按钮对象。以下是解决方案:

from __future__ import print_function
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.progressbar import ProgressBar
from kivy.uix.video import Video
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import WidgetException
from functools import partial


kv = """
<MainPage>:
    manager:screen_mgr
    do_default_tab: False
    textinputtext: waybill.text
    ScreenManager:
        id: screen_mgr
        #transition: FadeTransition
        Screen:
            manager: screen_mgr
            id: intro
            orientation: 'horizontal'
            name: 'User'
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size
            AnchorLayout:
                anchor_x: 'center'
                anchor_y: 'center'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 20
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'left'
                        size_hint_x: .5
                        TextInput:
                            id: waybill
                            width: 20
                            text: root.textinputtext
                            multiline: False
                            height: self.minimum_height
                            size_hint_y: None
                            font_size: 30
                            focus: True
                            on_text: self.text
                            on_text_validate:app.on_waybill()
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: "Compare"
                            on_release: root.compareClicked()
            AnchorLayout:
                anchor_x: 'right'
                anchor_y: 'bottom'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            id: statebtn
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: "State"
            AnchorLayout:
                anchor_x: 'left'
                anchor_y: 'top'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.5, 'center_y': .5}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            on_release:root.current = root.switch_screen()
                            text: "Admin"
    TabbedPanelHeader:
        text: intro.name
        # store a screen name to link the tab to a screen
        screen: intro.name
"""

Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)

class MainPage(TabbedPanel):

    screen_mgr = ObjectProperty(None)
    textinputtext = StringProperty()

    def __init__(self, **kwargs):
        super(MainPage, self).__init__(**kwargs)
        self.register_event_type('on_text')
        self.textinputtext = "Enter Waybill No."

    def on_text(self):
        return self.textinputtext

    def on_text_validate(self):
        return self.textinputtext

    def on_focus(self, obj, focused):
        if not focused:
            self.dispatch('on_text_validate')

    def compareClicked(self):
        self.progress_bar = ProgressBar()
        self.progress_bar.value = 1
        print (self.on_text())
        if self.on_text()!='Enter Waybill No.' :
            self.popup_1()

    def popup_1(self):
        self.box = BoxLayout(orientation='vertical', padding=(10))
        popup = Popup(title='Warning: Please Place', title_size=(30),
                      title_align='center', content=self.box,
                      size_hint=(None, None), size=(400, 400),
                      auto_dismiss=False)
        self.box.add_widget(Button(text="YES TO COMPARE", on_release=partial(self.addme)))
        self.box.add_widget(Button(text="NO TO GO BACK", on_release=popup.dismiss))
        popup.bind()
        popup.open()

    #@staticmethod
    def addme(self, button):
        print (self.on_text())

    def switch_screen(self):
        self.manager.transition = FadeTransition()
        self.manager.current = self.manager.previous()
        return self.manager.current

    def switch_to(self, header):
        self.manager.current = header.screen
        self.current_tab.state = "normal"
        header.state = 'down'
        self._current_tab = header

class KartScanApp(App):
    def build(self):
        return MainPage()

    def on_waybill(self):
        waybill.bind(text=MainPage.on_text)
        return waybill.text

if __name__ == '__main__':
    KartScanApp().run()

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2019-04-07
    • 2017-10-16
    • 1970-01-01
    • 2021-05-21
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多