2017-12-24 1 views
0

파이썬에서 앱을 만드는 데 상당히 익숙하지 않으며 다른 클래스 함수의 인수를 다른 클래스로 전달하는 방법을 알 수 없습니다. 내가 읽은 것을 바탕으로, 나는 초기화를 CustomPopup 클래스에 사용해야하지만 작동시키지 못했을 것입니다.다른 클래스에서 다른 클래스로 속성을 가져 오는 파이썬

파이썬 :

from kivy.app import App 
from kivy.lang import Builder 
from os import listdir 
from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.popup import Popup 
import teradata 
import Global 

class CustomPopup(Popup): 
    txt_input = 'Text' 

udaExec = teradata.UdaExec() 

kv_path = './kv/' 
for kv in listdir(kv_path): 
    Builder.load_file(kv_path+kv) 

class LoginScreen(Screen): 

    def buttonClicked(self): 
     Global.u_name = self.ids.u_name.text 
     Global.p_word = self.ids.p_word.text 
     status = None 
     try: 
      with udaExec.connect("${dataSourceName}",username=self.ids.u_name.text,password=self.ids.p_word.text) as session: 
       try: 
        session 
       except teradata.DatabaseError as e: 
        status = e.code 
     except teradata.DatabaseError as e: 
      status = e.code 

     if status == 8017: 
      self.popuper('Hello') 
     elif status == None: 
      self.popuper('All Good') 
     elif status == 0: 
      self.popuper('Fill in username') 
     else: 
      self.popuper('Something else') 

    def popuper(self, txt_input): 
     popuper = CustomPopup() 
     popuper.open() 

class MainScreen(Screen): 
    pass 

class ScreenManagement(ScreenManager): 
    pass 

application = Builder.load_file("main.kv") 

class MainApp(App): 

    def build(self): 
     self.title = 'Push Notfication Tool' 
     return application 

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

Kivy 여기

이 작동하지만 난 CustomPopup에 popuper에서 txt_input를 얻기 위해 노력하고 있지만,이 작업을 얻을 수 없다 내가 뭘하려고 오전의 제거 버전입니다 :

고마워요!

* EDIT에 kivy 파일도 추가되었습니다.

+0

(A [, 최소 완료하고 검증 가능한 예]를 제공하십시오 https://stackoverflow.com/help/ mcve) - 올바른 들여 쓰기와 구문을 사용하지 않으면 코드가 의미가 없습니다. 즉, atm이 작동하지 않습니다. -'__init__'은 f.e입니다. 여기에 covored : https://stackoverflow.com/a/625097/7505395 –

+0

[Python \ _ \ _ init \ _ \ _ 가능한 중복 및 어떻게할까요?] (https://stackoverflow.com/questions/625083)/python-init-and-self-what-do-they-do) –

+0

안녕하세요, 현재 코드의 전체 예제를 추가했습니다. 지금까지는 다른 모든 것들이 popuper에서 CustomPopup으로 txt_input을 얻는 방법을 알아낼 수 없다. – puputtiap

답변

0

보십시오 변화 몇 가지 :

class CustomPopup(Popup): 
    txt_input = StringProperty('Text') 

    def __init__(self, txt_input, **kw): 
     super().__init__(**kw) 
     self.txt_input = txt_input 

... 
    def popuper(self, txt_input): 
     popuper = CustomPopup(txt_input) # send txt_input! 
     popuper.open() 

이제 KV 파일

<CustomPopup>: # removed @Popup since you created it in python... 
    size_hint: (.5,.5) 
    auto_dismiss: False 
    title: "af" 
    BoxLayout: 
     orientation: 'vertical' 
     Label: 
      text: root.txt_input 
     Button: 
      text: "Close now" 
      on_press: root.dismiss() 
+0

트릭을 한 Yoav, 고마워! – puputtiap

관련 문제