如何从函数中的变量设置现有文本? Python Kivy [英] How to set existing text from a variable within a function? Python Kivy

查看:97
本文介绍了如何从函数中的变量设置现有文本? Python Kivy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个打开日志文件的函数,并将其保存到名为loginfo的变量中.在我的kivy文件中,我有一个TextInput小部件.我尝试将现有的text:设置为root.loginfo.

I created a function that opens a log file, and saves it to a variable named loginfo. In my kivy file, I have a TextInput widget. I tried setting the existing text: to root.loginfo.

loginfo需要在函数中,因为我使用的是Kivy的Clock重新读取日志文件.

The loginfo needs to be within a function because I am using the kivy's Clock to re-read the log file.

Python文件:

class Tasks(Screen):
    logginfo = ObjectProperty()

    def reset_text(dt):
        with open('logtest.log', 'r') as file:
            loginfo = file.read()

    Clock.schedule_once(reset_text, -1)

Kivy文件:

<Tasks>:
    name: 'task'
    logginfo: logginfo
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "TASKS"

        TextInput:
            id: logginfo
            text: root.loginfo

当我创建reset_text(dt)函数和kivy.clock时,问题开始出现.没有输入功能,仅显示内容,文本输入框会正确显示logtest.log文件的内容.

The problem started occurring when I created the reset_text(dt) function and kivy.clock. Without the function, and just the contents of it, the textinput box displays the logtest.log file's contents correctly.

当我运行脚本时,它会给我AttributeError: 'NoneType' object has no attribute 'replace'.我感到困惑和困惑,任何帮助将不胜感激.预先感谢.

When I run the script, it gives me AttributeError: 'NoneType' object has no attribute 'replace'. I'm confused and stuck, any help would be appreciated. Thanks in advance.

推荐答案

以下是完成您要执行的操作的完整示例.您必须对其进行修改以将其与您的代码集成,但是我的目的是向您展示实现此目标的正确方法,并让您自己使用它.

Here is a complete example to do what you want to do. You'll have to modify it to integrate it with your code, but my intention here was to show you the proper way to achieve this and let your work with it yourself.

请注意我如何使用Clock.schedule_interval而不是计划一次. schedule_interval中的1是调用self.reset_text函数之间的时间,以秒为单位.请注意如何在reset_text函数中使用self.root(GridLayout)在kv文件中引用我的基本窗口小部件,然后可以通过执行self.root.ids['my_text_input']来获取TextInput(因为我给了它一个ID)

Note how I'm using Clock.schedule_interval instead of schedule once. The 1 in the schedule_interval is the time between calling the self.reset_text function in seconds. Note how in the reset_text function I can refer to my base widget in my kv file using self.root (the GridLayout), then I can get the TextInput (since I gave it an id) by doing self.root.ids['my_text_input']

main.py

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder

GUI = Builder.load_file("main.kv")

class MainApp(App):
    def build(self):
        Clock.schedule_interval(self.reset_text, 1) # Check if log has changed once a second
        return GUI

    def reset_text(self, *args):
        with open("logtest.log", "r") as f:
            self.root.ids['my_text_input'].text = f.read()

MainApp().run()

main.kv

GridLayout:
    # This is the 'root' widget when referenced in python file
    cols: 1
    TextInput:
        id: my_text_input

这篇关于如何从函数中的变量设置现有文本? Python Kivy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆