繁体   English   中英

NameError: name 'from' 未定义

[英]NameError: name 'fro' not defined

我有下面显示的代码。 在函数 populate 中,我有变量来回、到和传输。 我试图将这些变量调用到 get 函数中,然后在显示弹出窗口时调用它们。 然后这些变量将显示在弹出窗口中。 但是,我有一个 NameError: name 'fro' is not defined with my code。 当我使用num = Connected.get()而不是num = g并注释掉c =Connected()g= c.get()我得到 < bound method MessageBox.get of <connected.MessageBox object at 0x000000000B36F3F0>>显示在 MessageBox 弹出窗口中。我不知道我做错了什么。 有没有更好的方法来做到这一点?

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

全局变量必须在 import afterstatements 之后定义。 导入 因为我没有你的完整代码。 下面的例子只是一个说明。

例子

主文件

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

测试.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()

输出

正在运行的应用程序

暂无
暂无

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

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