2017-12-02 1 views
1

기본 사용자 지정 DropDown을 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

관련 문제