2017-09-24 11 views
0

タイトルに記載されているように、私はビデオと2つのボタンでポップアップを作成しています - 今、これらのボタンの機能を実装したいと思います私が作成したポップアップからボタンが押されたときに呼び出すメソッドを作成しました - しかし、これは常にTypeErrorを返します:addme()は正確に1つの引数をとります(2) - このメソッドを静的にすると、AttributeError:オブジェクトは、これを解決するには、私を助けてくださいポップアップでボタンの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): 
     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() 

「on_text」は属性を持っていない、私は問題が何であるかを知るようになったここにいくつかの基本

+0

できるだけ少ないコード行に減らすことができますか?理想的には、Kivy以外の依存関係もありません。最初の一見から、 'on_text'は本当に奇妙に見えます。既に登録済みのように登録する必要はありません。あなたのコードを減らすなら、もっと深く見ていきます。 – PalimPalim

+0

こんにちは@ palimpalim、私は外部の依存関係を削除し、いくつかの不要なメソッドを削除しました、あなたはそれを見て、私は何が問題ですか? –

+0

数行しかなく、同じ問題がありますか? – PalimPalim

答えて

0

欠けています私のコードでは、ポップアップでボタンのon_releaseイベントからメソッドを呼び出すときはいつでも、メソッドは常にボタンオブジェクトを保持します - したがって、ボタンオブジェクトを保持できるもう1つの引数を受け入れるメソッドを作成しているとき。以下はその解決策です。

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() 
関連する問題