2017-12-19 1 views
0

이것은 쉬워야하는 것처럼 보이지만 클래스 (팝업에서 호출 됨)의 버튼 열을 업데이트하기 위해 일주일 동안 노력하고 있습니다.Kivy : 클래스의 위젯을 바깥에서 업데이트 하시겠습니까?

아래 코드를 실행하는 경우 맨 위 버튼 , 선택 팝업에서 "완료", 상단 버튼이 제거되지 않습니다 함수가 호출됩니다하지만

문제점 :

내가, 내가 할 수있는 클래스 JobDialog에서 클래스 MyWidget에 기능 refreshList 호출 할 때마다 함수가 실행 중인지 확인하십시오. 그러나 새 인스턴스가 열리고 그것이 무엇을 지는지 알 수 없다! 이것은 간단해야합니다. 나는 스파게티를 더 써서 어디에도 가지 않고있다!

입력 사항이 있습니까? APP 클래스에서 무언가를 정의해야합니까?

시간 내 주셔서 감사합니다. 여기

내가 가지고있는 코드입니다 (슈퍼는-간체) :

from kivy.uix.popup import Popup 
from kivy.lang import Builder 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
from kivy.app import App 
from kivy.config import Config 
from kivy.properties import ObjectProperty 
from kivy.uix.image import AsyncImage, Image 
from kivy.uix.label import Label 


Config.set('graphics', 'fullscreen', '0') #Force Fullscreen off. 

#setting up default variables 
loggedInUserName = "default usrname" 
isLoggedIn = 0 

currentProdNum = "" 

ResultSet = { 
    "1231" : {"name" : "test text asdf", "dateDue" : "", "status" : "0"}, 

} 


Builder.load_string(""" 

<MyWidget>: 
    BoxLayout: 
     ScrollView: 
      size_hint_x: 500 
      do_scroll_x: False 
      BoxLayout: 
       id: resultScrollList 
       cols: 1 
       size_hint_y: None 
       height: self.minimum_height 

""") 

class menuScreen(BoxLayout): 

    def __init__(self,**kwargs): 
     super(menuScreen,self).__init__(**kwargs) 
    pass 




class MyWidget(BoxLayout): 
    login = ObjectProperty() 
    def refreshList(self, *kwargs): 
     #for the scrollList:: 
     layout = GridLayout(cols=1, spacing=10, size_hint_y=None) 
     # Make sure the height is such that there is something to scroll. 
     layout.bind(minimum_height=layout.setter('height')) 

     self.orientation = "vertical" 

     self.name_input = TextInput(text='name') 

     #self.add_widget(self.name_input) 

     self.login_button = Button(text="login") 
     self.login_button.bind(on_press=self.login) 

     self.job_popup = JobDialog(self) # initiation of the popup, and self gets passed 

     #self.add_widget(self.login_button) 

     #start adding widgets: 

     for key, value in ResultSet.items(): 
      #first create the string for the box: 
      if value["status"] == "0": 
       print(key) 
       l = Label(text='Hello world', font_size='20sp') 
       strstr = value['name'] + ' - ' + value['dateDue'] + ' - ' + value['status'] 
       btn = Button(text=strstr,id=key, size_hint_y=None) 
       btn.bind(on_press=self.login) 
       layout.add_widget(btn) 
       pass 
      pass 

     self.ids.resultScrollList.clear_widgets() 
     #self.ids.resultScrollList.add_widget(layout) 
     #root.parent.MyWidget.ids.resultScrollList.add_widget(layout) 

    def __init__(self,**kwargs): 
     super(MyWidget,self).__init__(**kwargs) 

     #for the scrollList:: 
     layout = GridLayout(cols=1, spacing=10, size_hint_y=None) 
     # Make sure the height is such that there is something to scroll. 
     layout.bind(minimum_height=layout.setter('height')) 

     self.orientation = "vertical" 

     self.name_input = TextInput(text='name') 

     #self.add_widget(self.name_input) 

     self.login_button = Button(text="login") 
     self.login_button.bind(on_press=self.login) 

     self.job_popup = JobDialog(self) # initiation of the popup, and self gets passed 

     #self.add_widget(self.login_button) 

     #start adding widgets: 

     for key, value in ResultSet.items(): 
      #first create the string for the box: 
      print(key) 
      if value["status"] == "0": 
       l = Label(text='Hello world', font_size='20sp') 
       strstr = value['name'] + ' - ' + value['dateDue'] + ' - ' + value['status'] 
       btn = Button(text=strstr,id=key, size_hint_y=None) 
       btn.bind(on_press=self.login) 
       layout.add_widget(btn) 
       pass 
      pass 

     self.ids.resultScrollList.clear_widgets() 
     self.ids.resultScrollList.add_widget(layout) 

    def login(self, instance): 
     global isLoggedIn 
     global currentProdNum 
     currentProdNum = instance.id 
     print("current Prod to modify is %s" % currentProdNum) 
     self.job_popup.open() 
    pass 


class JobDialog(Popup): 
    global currentProdNum 
    print("current Prod to modify is %s" % str(currentProdNum)) 
    tempTitle = loggedInUserName 
    title = tempTitle 
    def __init__(self,my_widget,**kwargs): # my_widget is now the object where popup was called from. 
     super(JobDialog,self).__init__(**kwargs) 
     #my_widget.title='Authenticate' 
     self.my_widget = my_widget 
     #title='Authenticate', size_hint=(None, None), size=(400, 400) 
     self.content = BoxLayout(orientation="vertical") 
     self.title = "this is a test" 
     aimg = AsyncImage(source='https://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png') 

     self.done_button = Button(text='Done') 
     self.done_button.bind(on_press=self.DoneAction) 

     self.cancel_button = Button(text='Cancel') 
     self.cancel_button.bind(on_press=self.cancel) 

     self.pass_input = TextInput(text='') 

     self.content.add_widget(aimg) 
     self.content.add_widget(self.done_button) 
     self.content.add_widget(self.cancel_button) 


    def DoneAction(self,*args): 
     global loginLookupTable 
     global loggedInName 
     print(" %s selected!" % self.done_button.text) # and you can access all of its attributes 
     print("State of Prod:") 
     print(ResultSet[currentProdNum]['status']) 
     ResultSet[currentProdNum]['status'] = 1 
     print('Changed to 1.') 
     self.dismiss() 
     app = App.get_running_app() 
     app.mywidget.refreshList() 

    def cancel(self,*args): 
     print("cancel") 
     self.dismiss() 

class MyApp(App): 
    mywidget = MyWidget() 
    def build(self): 
     return MyWidget() 


MyApp().run() 
+1

문제가있는 간단한 실행 가능한 예제를 만들 수 있습니까? – EL3PHANTEN

+0

좋습니다. 보다 나은? – regor2

+0

refreshList 메서드 –

답변

0

마지막으로 그것을 알아 냈다! 수업 내용을 이해하지 못할 때 수업을 진행하는 것은 매우 어렵습니다. app.my_widget.refreshList() (App 클래스에서 정의했기 때문에 효과가 있음) 대신에 다른 클래스에서 "self"를 상속 받도록 정의한 후에 self.my_widget.refreshList()을 호출했습니다. 소리가 간단합니다. 관심있는 사람들을위한 작업 코드는 다음과 같습니다.

from kivy.uix.popup import Popup 
from kivy.lang import Builder 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.button import Button 
from kivy.uix.textinput import TextInput 
from kivy.app import App 
from kivy.config import Config 
from kivy.properties import ObjectProperty 
from kivy.uix.image import AsyncImage, Image 
from kivy.uix.label import Label 


Config.set('graphics', 'fullscreen', '0') #Force Fullscreen off. 

#setting up default variables 
loggedInUserName = "default usrname" 
isLoggedIn = 0 

currentProdNum = "" 

ResultSet = { 
    "1231" : {"name" : "test text asdf", "dateDue" : "", "status" : "0"}, 

} 


Builder.load_string(""" 

<MyWidget>: 
    BoxLayout: 
     ScrollView: 
      size_hint_x: 500 
      do_scroll_x: False 
      BoxLayout: 
       id: resultScrollList 
       cols: 1 
       size_hint_y: None 
       height: self.minimum_height 

""") 

class menuScreen(BoxLayout): 

    def __init__(self,**kwargs): 
     super(menuScreen,self).__init__(**kwargs) 
    pass 




class MyWidget(BoxLayout): 
    login = ObjectProperty() 
    def refreshList(self, *kwargs): 
     #for the scrollList:: 
     layout = GridLayout(cols=1, spacing=10, size_hint_y=None) 
     # Make sure the height is such that there is something to scroll. 
     layout.bind(minimum_height=layout.setter('height')) 

     self.orientation = "vertical" 

     self.name_input = TextInput(text='name') 

     #self.add_widget(self.name_input) 

     self.login_button = Button(text="login") 
     self.login_button.bind(on_press=self.login) 

     self.job_popup = JobDialog(self) # initiation of the popup, and self gets passed 

     #self.add_widget(self.login_button) 

     #start adding widgets: 

     for key, value in ResultSet.items(): 
      #first create the string for the box: 
      if value["status"] == "0": 
       print(key) 
       l = Label(text='Hello world', font_size='20sp') 
       strstr = value['name'] + ' - ' + value['dateDue'] + ' - ' + value['status'] 
       btn = Button(text=strstr,id=key, size_hint_y=None) 
       btn.bind(on_press=self.login) 
       layout.add_widget(btn) 
       pass 
      pass 

     self.ids.resultScrollList.clear_widgets() 
     #self.ids.resultScrollList.add_widget(layout) 
     #root.parent.MyWidget.ids.resultScrollList.add_widget(layout) 

    def __init__(self,**kwargs): 
     super(MyWidget,self).__init__(**kwargs) 

     #for the scrollList:: 
     layout = GridLayout(cols=1, spacing=10, size_hint_y=None) 
     # Make sure the height is such that there is something to scroll. 
     layout.bind(minimum_height=layout.setter('height')) 

     self.orientation = "vertical" 

     self.name_input = TextInput(text='name') 

     #self.add_widget(self.name_input) 

     self.login_button = Button(text="login") 
     self.login_button.bind(on_press=self.login) 

     self.job_popup = JobDialog(self) # initiation of the popup, and self gets passed 

     #self.add_widget(self.login_button) 

     #start adding widgets: 

     for key, value in ResultSet.items(): 
      #first create the string for the box: 
      print(key) 
      if value["status"] == "0": 
       l = Label(text='Hello world', font_size='20sp') 
       strstr = value['name'] + ' - ' + value['dateDue'] + ' - ' + value['status'] 
       btn = Button(text=strstr,id=key, size_hint_y=None) 
       btn.bind(on_press=self.login) 
       layout.add_widget(btn) 
       pass 
      pass 

     self.ids.resultScrollList.clear_widgets() 
     self.ids.resultScrollList.add_widget(layout) 

    def login(self, instance): 
     global isLoggedIn 
     global currentProdNum 
     currentProdNum = instance.id 
     print("current Prod to modify is %s" % currentProdNum) 
     self.job_popup.open() 
    pass 


class JobDialog(Popup): 
    global currentProdNum 
    print("current Prod to modify is %s" % str(currentProdNum)) 
    tempTitle = loggedInUserName 
    title = tempTitle 
    def __init__(self,my_widget,**kwargs): # my_widget is now the object where popup was called from. 
     super(JobDialog,self).__init__(**kwargs) 
     #my_widget.title='Authenticate' 
     self.my_widget = my_widget 
     #title='Authenticate', size_hint=(None, None), size=(400, 400) 
     self.content = BoxLayout(orientation="vertical") 
     self.title = "this is a test" 
     aimg = AsyncImage(source='https://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png') 

     self.done_button = Button(text='Done') 
     self.done_button.bind(on_press=self.DoneAction) 

     self.cancel_button = Button(text='Cancel') 
     self.cancel_button.bind(on_press=self.cancel) 

     self.pass_input = TextInput(text='') 

     self.content.add_widget(aimg) 
     self.content.add_widget(self.done_button) 
     self.content.add_widget(self.cancel_button) 


    def DoneAction(self,*args): 
     global loginLookupTable 
     global loggedInName 
     print(" %s selected!" % self.done_button.text) # and you can access all of its attributes 
     print("State of Prod:") 
     print(ResultSet[currentProdNum]['status']) 
     ResultSet[currentProdNum]['status'] = 1 
     print('Changed to 1.') 
     self.dismiss() 
     self.my_widget.refreshList() 

    def cancel(self,*args): 
     print("cancel") 
     self.dismiss() 

class MyApp(App): 
    mywidget = MyWidget() 
    def build(self): 
     return MyWidget() 


MyApp().run() 
관련 문제