【问题标题】:Kivy Does Not Output Plain Text, Only Object LocationKivy 不输出纯文本,只输出对象位置
【发布时间】:2019-02-27 07:36:06
【问题描述】:

所以我试图在 Kivy 中创建一个非常基本的文本编辑器。所以我解决了文本不显示的问题(通过 lambda)。然而,一个新的错误出现了。保存。我想自动保存到纯 txt 文件。但是,我当前的代码只保存实际文本输入的一个对象(它是一个指针吗?)。谢谢大家,所以!

    import kivy
    import os
    kivy.require('1.10.1') # replace with your current kivy version !
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.textinput import TextInput
    from kivy.uix.label import Label
    from kivy.clock import Clock
    from kivy.uix.button import Button
    from kivy.lang import Builder

    class ColdKivyApp(App): # I actually used to call it Zone but changed it to Cold cause it's cold outside ;)
            def build(self):
                    f = BoxLayout(orientation='vertical')
                    txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
                    f.add_widget(txt)
                    txtstr = str(txt)
                    Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
                    with open('testtxt.txt', 'w') as txtwriter:
                        txtwriter.write("" + txtstr)
                        txtwriter.close()
                    return f

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

编辑:拼写

【问题讨论】:

  • 如 Tshirtman 的回答中所述,使用 txt.text 访问您的 txt 变量的文本。您还可以使用txt.text = 'foo' 设置他们的文本。其他可以使用.text 的 kivy 小部件包括按钮和标签。

标签: python python-3.x text file-io kivy


【解决方案1】:

TextInput 有一个text 属性,这是当前内容所在的位置。

【讨论】:

  • 那么怎么写成txt文件呢?
  • 只是python中的常用方式:with open(filename, 'w') as fd: fd.write(text)
  • 如果您不澄清什么不起作用(代码中断、您遇到的错误等),则无法为您提供帮助
  • 无文本输出,创建文件但未写入文本。抱歉耽搁了!
【解决方案2】:

将代码中的txtstr = str(txt) 替换为txtstr = txt.text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2011-08-20
    相关资源
    最近更新 更多