Search code examples
python-3.xkivykivy-language

NameError: name 'fro' not defined


I have the code shown below. In the function populate I have the variables fro, to and transport. I am trying to call these variables into the get function thereafter call them when a Popup is display. These variables will then be displayed in the popup. However, I am having a NameError: name 'fro' is not defined with my code. When i use num = Connected.get() instead of num = g and comment out c =Connected() and g= c.get() i get <bound method MessageBox.get of <connected.MessageBox object at 0x000000000B36F3F0>> displayed in the MessageBox Popup.I don't know what I am doing wrong. Is there a better way to do it?

    class Connected(Screen):

    def populate(self, transText, beginText, toText):
        global fro
        global to
        global transport

        self.rv.data = []
        self.ids.results.text=""
        self.ids.no_entry.text=""

        fro = beginText
        to = toText
        transport = transText        


    def get(self):
        b = fro
        a = StringProperty(b)
        return a

class MessageBox(Popup):
    c = Connected()
    g = c.get()

    route = 'MessageBox'

    num = g
    #num = Connected.get() 

    def popup_dismiss(self):
        self.dismiss()

Kivy.kv

<MessageBox>:
    title: root.route
    size_hint: None, None
    size: 400, 400
    separator_color: 1,0,0,1

    BoxLayout:
        orientation: 'vertical'
        Label:
            id: info
            #text: 'nice'
            text: str(root.num)
        Button:
            size_hint: 1, 0.2
            text: 'OK'
            on_press:
                root.dismiss()

Solution

  • Global variables must be defined after the import afterstatements. the import Since I don't have your complete codes. The example below is just an illustration.

    Example

    main.py

    from kivy.app import App
    from kivy.uix.screenmanager import Screen
    from kivy.uix.popup import Popup
    from kivy.properties import StringProperty
    
    
    fro = "fro-1-2-3"
    to = "to-1-2-3"
    transport = "transport-1-2-3"
    
    
    class Connected(Screen):
    
        def __init__(self, **kwargs):
            super(Connected, self).__init__(**kwargs)
            self.populate("transText", "beginText", "toText")
    
        def populate(self, transText, beginText, toText):
            global fro
            global to
            global transport
    
            # self.rv.data = []
            # self.ids.results.text = ""
            # self.ids.no_entry.text = ""
    
            fro = beginText
            to = toText
            transport = transText
    
        def get(self):
            print("Connected.get: from={}".format(fro))
            b = fro
            a = StringProperty(b)
            return a
    
    
    class MessageBox(Popup):
        c = Connected()
        g = c.get()
        print("MessageBox: g={}".format(g))
    
        route = 'MessageBox'
    
        num = g
        #num = Connected.get()
    
        def popup_dismiss(self):
            self.dismiss()
    
    
    class TestApp(App):
        def build(self):
            return MessageBox()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    
    <Connected>:
    
    <MessageBox>:
        title: root.route
        size_hint: None, None
        size: 400, 400
        separator_color: 1,0,0,1
    
        BoxLayout:
            orientation: 'vertical'
            Label:
                id: info
                #text: 'nice'
                text: str(root.num)
            Button:
                size_hint: 1, 0.2
                text: 'OK'
                on_press:
                    root.dismiss()
    

    Output

    Apps Running