Mimic Tkinter文件在Kivy中打开

时间:2018-04-04 21:20:47

标签: python kivy

之前我主要使用过Tkinter,并使用以下代码存储我选择的文件的路径。在探索Kivy之后,Kivy似乎没有像这样的简单功能。所以我尝试使用filechoser

[191 125  84]  # from img
[191 125  84]  # from c 
[191 125  84]  # etc.
[191 125  84]
[185 119  78]
[185 119  78]
[184 118  77]
[184 118  77]
[188 122  81]
[188 122  81]
[189 123  82]
[189 123  82]
[188 122  81]
[188 122  81]
[190 124  83]
[190 124  83]
[190 126  85]
[190 126  85]

主要代码非常简单

path = filedialog.askopenfilename(initialdir="/", title="Select file")

然后我创建了一个.kv文件,只是为了查看filechooser的样子。该脚本只会打开并冻结。

class checker_ui(GridLayout):
    def findreport(self,path):
        pass



class Checker(App):
    def build(self):
        return checker_ui()

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

这是输出

Freeze

我搜遍了整个地方,没有看到有类似问题的人。有没有其他我可以尝试模仿tkinter行为或我是否坚持这个问题?我在Windows机器上,普通的kivy小部件可以工作,即:按钮,标签等。

3 个答案:

答案 0 :(得分:0)

所以我写了比你更多的代码(很大程度上基于kivy示例@ https://kivy.org/docs/api-kivy.uix.filechooser.html#usage-example

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.lang import Builder

Builder.load_string("""
#:kivy 1.1.0

<Root>:
    text_input: text_input

    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: 'Load'
                on_release: root.show_load()
            Button:
                text: 'Save'
                on_release: root.show_save()

        BoxLayout:
            TextInput:
                id: text_input
                text: ''

            RstDocument:
                text: text_input.text
                show_errors: True

<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        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)

<SaveDialog>:
    text_input: text_input
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser
            on_selection: text_input.text = self.selection and self.selection[0] or ''

        TextInput:
            id: text_input
            size_hint_y: None
            height: 30
            multiline: False

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Save"
                on_release: root.save(filechooser.path, text_input.text)""")
import os


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


class SaveDialog(FloatLayout):
    save = ObjectProperty(None)
    text_input = ObjectProperty(None)
    cancel = ObjectProperty(None)


class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    text_input = ObjectProperty(None)

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

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def show_save(self):
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        with open(os.path.join(path, filename[0])) as stream:
            self.text_input.text = stream.read()

        self.dismiss_popup()

    def save(self, path, filename):
        with open(os.path.join(path, filename), 'w') as stream:
            stream.write(self.text_input.text)

        self.dismiss_popup()


class Editor(App):
    def build(self):
        return Root()




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

答案 1 :(得分:0)

解决方案

  1. FileChooserIconLayout 替换为 FileChooserIconView
  2. 添加 BoxLayout 并使 BoxChooserIconView 成为 BoxLayout 的子项
  3. 实施例

    main.py

    public class Result
    {
        public string ActivityIdentifier { get; set; }
        public string CharacteristicName { get; set; }
        public bool SubmitData { get; set; }
    }
    
    public class ApiModel
    {
        public List<Result> Results { get; set; }
    }
    
    public class RootObject
    {
        public ApiModel apiModel { get; set; }
    }
    

    checker.kv

    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    
    
    class Checker_ui(GridLayout):
    
        def findreport(self, path):
            print("path=", path)
    
    
    class Checker(App):
    
        def build(self):
            return Checker_ui()
    
    
    if __name__ == "__main__":
        Checker().run()
    

    输出

    Img01 - AppStartup

答案 2 :(得分:0)

嘿,如果有人想打开所有类型的文件,只需使用(mp4、jpg、pdf、txt、docs...等)

导入浏览器

webbrowser.open(文件名[0])