0

Fairly new to building apps in Python and I cannot figure out how to pass argument from other class function to other class. Based on what i read, i guess I should use def init in CustomPopup class but haven't been able to get it working.

Here is a stripped version of what i am trying to do, this works but i am trying to get txt_input from popuper to CustomPopup but cannot get it working:

Python:

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@Popup>:
    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()

Thank you!

*EDIT added kivy file as well.

puputtiap
  • 21
  • 8
  • please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) - your code does not make much sense without the correct indentation and syntax - in short It is not working atm. - `__init__` is f.e. covored here: https://stackoverflow.com/a/625097/7505395 – Patrick Artner Dec 24 '17 at 08:23
  • Possible duplicate of [Python \_\_init\_\_ and self what do they do?](https://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do) – Patrick Artner Dec 24 '17 at 08:27
  • Hi, added full example of current code. Everything else is working so far other than i cannot figure out how to get txt_input from popuper to CustomPopup. – puputtiap Dec 24 '17 at 09:23
  • popuper = CustomPopup(txt_input). in CustomPopup: def __init__(self, txt_input): self.txt_input = txt_input... i guess you can use 'super()' to bind your input to popup if needed. usefull: https://kivy.org/docs/api-kivy.uix.popup.html , https://stackoverflow.com/questions/3694371/how-do-i-initialize-the-base-super-class – yeg Dec 24 '17 at 11:40
  • Hi yeg. Thank you for the links. Tried to modify as suggested but then python crashes completely without any error. I will check the links and try to find a working solution. – puputtiap Dec 24 '17 at 12:55
  • please provide the kv file + try to make the example more slim – Yoav Glazner Dec 26 '17 at 22:14
  • I couldn't get it working through kivy so i just made function directly in my class. Works fine but i will definitely look in to it more later as that would allow me to use the same popup everywhere in my app. `def popuper(self, txt_input): box = BoxLayout(orientation='vertical') box.add_widget(Label(text=txt_input)) btn = Button(text='Close',size_hint=(1,0.3)) box.add_widget(btn) pop = Popup(title='Error', content=box, size_hint=(None,None),size=(400,200)) btn.bind(on_press=pop.dismiss) pop.open()` – puputtiap Jan 04 '18 at 12:07
  • Hi @YoavGlazner. I added the kivy part of the custompopup – puputtiap Jan 04 '18 at 13:58

1 Answers1

0

Try changing a few things:

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()

now the kv file

<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()
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36