简体   繁体   English

NameError: name 'from' 未定义

[英]NameError: name 'fro' not defined

I have the code shown below.我有下面显示的代码。 In the function populate I have the variables fro, to and transport.在函数 populate 中,我有变量来回、到和传输。 I am trying to call these variables into the get function thereafter call them when a Popup is display.我试图将这些变量调用到 get 函数中,然后在显示弹出窗口时调用它们。 These variables will then be displayed in the popup.然后这些变量将显示在弹出窗口中。 However, I am having a NameError: name 'fro' is not defined with my code.但是,我有一个 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.当我使用num = Connected.get()而不是num = g并注释掉c =Connected()g= c.get()我得到 < bound method MessageBox.get of <connected.MessageBox object at 0x000000000B36F3F0>>显示在 MessageBox 弹出窗口中。我不知道我做错了什么。 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 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()

Global variables must be defined after the import afterstatements.全局变量必须在 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测试.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输出

正在运行的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM