2016-01-22 21 views
1

我正在編寫一個kivy應用程序,我希望將佈局和控制器分開。這裏是什麼,我試圖完成的示例代碼:kivy gui中的單獨佈局和控制器

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
from kivy.uix.label import Label 


class AppLayout(BoxLayout): 
    def __init__(self, **kwargs): 
    super(AppLayout, self).__init__(**kwargs) 
    self.orientation = 'vertical' 
    self.padding = [10, 10, 10, 10] 
    self.text_bar = TextInput(hint_text='Write Something', 
           multiline=False, 
           padding=[5, 5, 5, 5], 
           font_size=20, 
           size_hint=(0.8, 1)) 
    self.button = Button(text="Do Something", 
           size_hint=(0.2, 1)) 

    self._text_bar_box = BoxLayout(pos_hint={'top': 1}, 
            size_hint=(1, 0.04), 
            orientation='horizontal') 
    self._text_bar_box.add_widget(self.text_bar) 
    self._text_bar_box.add_widget(self.button) 
    self.add_widget(self._text_bar_box) 

    self.label = Label(text="Your text will be displayed here", font_size=20) 
    self._big_box = BoxLayout(padding=[10, 10, 10, 10], 
           size_hint=(1, 0.65)) 
    self._big_box.add_widget(self.label) 
    self.add_widget(self._big_box) 


class AppController: 
    def __init__(self, app_layout: AppLayout): 
     super(AppController, self).__init__() 
     self.app_layout = app_layout 
     self.app_layout.search_button.bind(on_press=self.get_string) 

    def get_string(self): 
     self.app_layout.label.text = self.app_layout.text_bar.text 


class MainApp(App): 
    def build(self): 
     app_layout = AppLayout() 
     AppController(app_layout) 
     return app_layout 


if __name__ == "__main__": 
    MainApp().run() 

這是怎麼出現的GUI: Here is what the the GUI looks like 當我按下buttontext_bar中的文本標籤不顯示。我怎樣才能做到這一點?

回答

2

有與您發佈的例如一些其他的問題,它卻是真正打破了這裏

class MainApp(App): 
    def build(self): 
     app_layout = AppLayout() 
     AppController(app_layout) 
     return app_layout 

AppController實例被垃圾立即收集。它另存爲的MainApp屬性:

self.controller = AppController(app_layout) 

編輯:完整的示例

回調預計,一個額外的參數,instance:引發該事件的對象。

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
from kivy.uix.label import Label 

class AppLayout(BoxLayout): 
    def __init__(self, **kwargs): 
     super(AppLayout, self).__init__(**kwargs) 
     self.orientation = 'vertical' 
     self.padding = [10, 10, 10, 10] 
     self.text_bar = TextInput(hint_text='Write Something', 
            multiline=False, 
            padding=[5, 5, 5, 5], 
            font_size=20, 
            size_hint=(0.8, 1)) 
     self.button = Button(text="Do Something", 
            size_hint=(0.2, 1)) 

     self._text_bar_box = BoxLayout(pos_hint={'top': 1}, 
             size_hint=(1, 0.04), 
             orientation='horizontal') 
     self._text_bar_box.add_widget(self.text_bar) 
     self._text_bar_box.add_widget(self.button) 
     self.add_widget(self._text_bar_box) 

     self.label = Label(text="Your text will be displayed here", font_size=20) 
     self._big_box = BoxLayout(padding=[10, 10, 10, 10], 
            size_hint=(1, 0.65)) 
     self._big_box.add_widget(self.label) 
     self.add_widget(self._big_box) 


class AppController: 
    def __init__(self, app_layout= AppLayout): 
     self.app_layout = app_layout 
     self.app_layout.button.bind(on_press=self.get_string) 

    def get_string(self, *args): 
     self.app_layout.label.text = self.app_layout.text_bar.text 


class MainApp(App): 

    def build(self): 
     app_layout = AppLayout() 
     self._controller = AppController(app_layout) 
     return app_layout 


if __name__ == "__main__": 
    MainApp().run() 
+0

'類MainApp(APP): 高清建(個體經營): self.app_layout = AppLayout() self.controller = AppController中(self.app_layout) 回報self.app_layout' 我改變了MainApp類,但現在我得到以下錯誤: 'TypeError:get_string()需要1個位置參數,但2個被給予' 您能指出問題和修復嗎? – Pacu

+0

@Pacu請參閱編輯。 – zeeMonkeez

+0

感謝您的修復。它現在完美。 – Pacu