Calling a method from on_release event of a button in a pop up

64 views
Skip to first unread message

శంకర్ కుమార్ యల్లపు

unread,
Sep 25, 2017, 2:59:20 AM9/25/17
to Kivy users support
As I mentioned in the title, I am creating a pop up with a video and two buttons - Now I want to implement functionality for those buttons - so, I have created a method to call when a button pressed from the pop up I have created - But this always returns TypeError: addme() takes exactly 1 argument (2 given) - If I make this method as static then it returns 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
import re, wmi
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
                    source: 'index.png'
            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: root.getusbdevices()
            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)
adwaybill = TextInput(text="Enter Waybill No.", multiline=False)
statebtn = Button(text="INSTRUMENT_NOT_FOUND")
player1 = Video(source='connect.mp4')
player2 = Video(source='place.mp4')
waybills = []

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.' and self.getusbdevices() == u"CONNECTED":
            self.popup_1()
#I'm trying to call addme method here
    def popup_1(self):
        self.box = BoxLayout(orientation='vertical', padding=(10))
        self.box.add_widget(self.playPlace())
        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 playConnect(self):
        player1.state = 'play'
        return player1

    def playPlace(self):
        player2.state = 'play'
        return player2

    def getusbdevices(self):
        c = wmi.WMI()
        statebtn.text = u"INSTRUMENT_NOT_FOUND"
        wql = "Select * From Win32_USBControllerDevice"
        for item in c.query(wql):
            if "USB Device" in item.Dependent.Caption:
                statebtn.text = u"CONNECTED"
            else:
                statebtn.text = u"INSTRUMENT_NOT_FOUND"
        return statebtn.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()

ZenCODE

unread,
Sep 26, 2017, 4:06:54 AM9/26/17
to Kivy users support
The button release event takes one parameter, then you need the seld:

    def add_me(self, button):
        pass
Reply all
Reply to author
Forward
0 new messages