【问题标题】:Append string to textinput on button release using kivy使用 kivy 在按钮释放时将字符串附加到文本输入
【发布时间】:2019-10-26 15:27:04
【问题描述】:

我有一种使用kivy filechooser 选择文件的方法,我想在点击Load 按钮后将读取的文件内容加载到TextInput 字段中。

我不确定我是否在.kv 文件中正确使用变量text_from_file,但我被困在这一点上,不知道还能在这里做什么。

我尝试将文件中的字符串存储到self.text_from_file 中,然后将其分配给self.usecase 变量,在.kv 文件中,我使用usecase 映射textinput 字段。但是在我点击Load 按钮后,该字符串没有出现在textinput 字段中。

这是我的python代码(main.py)

class Grid(Widget):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
text_from_file = ObjectProperty(None)
usecase = ObjectProperty(None)

def show_load_list(self):
    content = LoadDialog(load=self.load_list, cancel=self.dismiss_popup)
    self._popup = Popup(title="Load a file list", content=content, size_hint=(1, 1))
    self._popup.open()

def load_list(self, path, filename):
    with open(filename[0], 'r') as file:
        self.text_from_file = file.read()
    self.usecase = self.text_from_file
    self.dismiss_popup()

def dismiss_popup(self):
    self._popup.dismiss()

这是main.kv

<Grid>
    usecase: use_case_text

    GridLayout:
        cols:1
        size: root.width, root.height

        GridLayout:
            cols:2

            TextInput:
                id: use_case_text
                multiline: True

        Button:
            text: "Import use case"
            on_press: root.show_load_list()


<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            path: './'
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()
            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    我发现我缺少.textObjectProperty() 部分:

    self.text_from_file.text = string

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-04
      • 2014-02-25
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2016-08-29
      相关资源
      最近更新 更多