2017-12-02 4 views
1

私はkvファイルに基本カスタムドロップダウンを定義します。アプリのGUIは非常にシンプルで、ボタンバーは上部、TextInputは残りの部分を消費します。ここでは、コードです:DropDown open()メソッドでカスタムDropDownを開くことができません

dropdowntrialgui.py

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.dropdown import DropDown 

class CustomDropDown(DropDown): 
    pass 

class DropDownTrialGUI(BoxLayout): 
    dropD = CustomDropDown() 

    def openMenu(self, widget): 
     self.dropD.open(widget) 


class DropDownTrialGUIApp(App): 
    def build(self): 
     return DropDownTrialGUI() 


if __name__== '__main__': 
    dbApp = DropDownTrialGUIApp() 

    dbApp.run() 

とKVファイル:

dropdowntrialgui.kvはmenuBtnを押す

DropDownTrialGUI: 

    <CustomDropDown> 
     Button: 
      text: 'My first Item' 
      size_hint_y: None 
      height: '28dp' 
      on_release: root.select('item1') 
     Button: 
      text: 'My second Item' 
      size_hint_y: None 
      height: '28dp' 
      on_release: root.select('item2') 

    <DropDownTrialGUI>: 
     orientation: "vertical" 
     padding: 10 
     spacing: 10 

     BoxLayout: 
      size_hint_y: None 
      height: "28dp" 
      Button: 
       id: toggleHistoryBtn 
       text: "History" 
       size_hint_x: 15 
      Button: 
       id: deleteBtn 
       text: "Delete" 
       size_hint_x: 15 
      Button: 
       id: replaceBtn 
       text: "Replace" 
       size_hint_x: 15 
      Button: 
       id: replayAllBtn 
       text: "Replay All" 
       size_hint_x: 15 
      Button: 
       id: menuBtn 
       text: "..." 
       size_hint_x: 15 
       on_press: root.openMenu(self) 

     TextInput: 
      id: readOnlyLog 
      size_hint_y: 1 
      readonly: True 

は効果がありません。どのように問題を解決できますか?

答えて

1

一般的なルールとしてクラス属性(kivyプロパティを除く)として何も定義しないで、代わりに__init__メソッドでインスタンス化することでウィジェットをインスタンス属性として定義する必要があります。

class DropDownTrialGUI(BoxLayout): 
    def __init__(self, **kwargs): 
     super(DropDownTrialGUI, self).__init__(**kwargs) 
     self.dropD = CustomDropDown() 

    def openMenu(self, widget): 
     self.dropD.open(widget) 

enter image description here

関連する問題