Kivy没有在文本fi中存储数据

2024-04-26 20:44:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个应用程序注册,但我无法将结果存储在文本文件中。这是我的密码。我想存储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()

Tags: ofthetextnamefromimportselfid
1条回答
网友
1楼 · 发布于 2024-04-26 20:44:18

在self.result.text是一个包含字符串的变量。因此,在f.write()语句中,不需要引号self.result. 相反,它应该看起来像:

f.写入(self.result.text)在

KC公司

相关问题 更多 >