2017-09-24 70 views
0

正如我在標題中提到的,我創建了一個彈出式視頻和兩個按鈕 - 現在我想實現這些按鈕的功能 - 所以,我有創建一個方法來調用當我彈出一個按鈕按下時創建 - 但這總是返回TypeError:addme()只需要1個參數(給出2) - 如果我把這個方法作爲靜態,那麼它返回AttributeError:'Button'對象有「on_text」從彈出的按鈕的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() 

沒有屬性請幫我解決這個,我錯過了一些基礎知識在這裏

+0

您能否將您的示例簡化爲儘可能少的代碼行。理想情況下,除了kivy之外也沒有依賴關係。從第一次看,'on_text'看起來很奇怪,也沒有必要註冊爲已經註冊。如果你減少你的代碼,我會更深入地研究。 – PalimPalim

+0

嗨@ palimpalim,我已經刪除了外部依賴關係,並刪除了一些不必要的方法,請你看看它並建議我有什麼問題? –

+0

難道你不能舉一個例子,只是幾行,並有同樣的問題? – PalimPalim

回答

0

我才知道這是什麼問題用我的代碼,每當我從彈出的按鈕的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() 
相關問題