【问题标题】:Mimic Tkinter file open in Kivy在 Kivy 中打开 Mimic Tkinter 文件
【发布时间】:2018-04-04 21:20:46
【问题描述】:

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

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

主要代码很简单

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



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

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

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

<checker_ui>:
    BoxLayout:
        FileChooserIconLayout:
            id:filechooser
            on_selection:root.select(*args)

这是输出

我到处搜索,没有看到任何人遇到类似问题。是否有替代方法可以尝试模仿 tkinter 行为,或者我是否遇到了这个问题?我在 Windows 机器上,正常的 kivy 小部件可以工作,即:按钮、标签等。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    所以我写的代码比你多很多(主要基于 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()
    

    【讨论】:

    • 我确实试过这个例子,点击加载后机器会死机。我试图用一种极简主义的策略来看看它是否能这样工作。可能与我的机器有关。
    • 嗯,它对我有用......你复制并粘贴了这个,根本没有编辑它,它冻结了?您使用的是旧版本的 kivy 吗?你确定你安装了所有的 kivy 依赖项吗?你在用普通的 CPython 运行吗?什么版本的 Python?你是通过cygwin还是什么的?您是否正在运行其他一些 python 变体(anaconda/cython/pipython/etc)?
    • 我重新启动并加载它,但是当我单击时文件名开始相互重叠。
    • 在阅读了示例并操作了几行之后,我能够让它工作。虽然它正在工作,但我得到了这个错误 pywintypes.error: (32, 'GetFileAttributesEx', '进程无法访问文件,因为它正被另一个进程使用。')你以前见过这个吗?
    【解决方案2】:

    解决方案

    1. FileChooserIconLayout 替换为 FileChooserIconView
    2. 添加一个 BoxLayout 并使 FileChooserIconView 成为 BoxLayout 的子项

    示例

    main.py

    ​​>
    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()
    

    checker.kv

    #:kivy 1.10.0
    
    <Checker_ui>:
        BoxLayout:
            size: root.size
            pos: root.pos
            orientation: "vertical"
            FileChooserIconView:
                id: filechooser
                on_selection: root.select(*args)
    

    输出

    【讨论】:

    • 选择文件后出现错误。变量路径似乎与任何东西无关。
    【解决方案3】:

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

    导入浏览器

    webbrowser.open(文件名[0])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2018-01-03
      • 2019-12-24
      • 2018-02-26
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多