Kivy不将数据存储在文本文件中

时间:2014-04-05 14:08:47

标签: python kivy

我正在尝试创建一个注册应用,但我无法将结果存储在文本文件中。这是我的代码。我想将self.result.text的值存储在文本文件中,但我的代码不是写这个。也没有错误。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
import xml.etree.cElementTree as ET
import fileinput
import sys

Builder.load_string("""
<Reg>:
    # This are attributes of the class Reg now
    a: _a
    b: _b
    c: _c
    d: _d
    e: _e
    f: _f
    g: _g
    result: _result
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                GridLayout:
                    cols:1
                    TextInput:
                        id: _a
                        text: 'Name: '
                    TextInput:
                        id: _b
                        text: 'Age: '
                    TextInput:
                        id: _c
                        text: 'Phone: '
                    TextInput:
                        id: _d
                        text: 'Email: '
                    TextInput:
                        id: _e
                        text: 'Address: '   
                    TextInput:
                        id: _f
                        text: 'Guardian Name: '
                    TextInput:
                        id: _g
                        text: 'Guardian Phone: '                            
                    Label:
                        id: _result

                    Button:
                        text: 'Register Me'
                        # Or you can call a method from the root class (instance of calc)
                        on_press: root.genxml(*args)
            Screen:
                name: 'screen2'
                Label: 
                    text: 'The second screen'
""")

class Reg(FloatLayout):
    # define the multiplication of a function
    def genxml(self, instance):
        #self.result, self.a and self.b where defined explicitely in the kv
        self.result.text = self.a.text + self.b.text + self.c.text
        with open('output.txt', 'w') as f:
            f.write("self.result.text")  << this line is not creating file
            f.close()
        #pass

class RegistrationApp(App):
    def build(self):
        return Reg()

if __name__ == '__main__':
    RegistrationApp().run()

1 个答案:

答案 0 :(得分:0)

self.result.text是一个包含字符串的变量。因此,在f.write()声明中,您不需要&#34;引用&#34;围绕self.result.text。相反它应该看起来像:

f.write(self.result.text)

KC