使用FileChooser选择文件夹

使用FileChooser选择文件夹

本文介绍了使用FileChooser选择文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Kivy Filechooser添加到gridlayout中 https://kivy.org/docs/api-kivy.uix.filechooser. html

i want to add kivy filechooser into gridlayouthttps://kivy.org/docs/api-kivy.uix.filechooser.html

我有我的主要班级:

class MainApp(GridLayout):
    def __init__(self, **kwargs):
        mylayout = BoxLayout(orientation='vertical')

我想将filechooser中的编辑器类添加到mylayout BoxLayout中如果我添加

i want to add editor class in filechooser into mylayout BoxLayoutif i add

mylayout.add_widget(Editor.run())

我在全屏窗口中有文件选择器,而不是boxlayout

I have filechooser in fullscreen of my window, not in boxlayout

我希望该用户可以选择文件夹(而非文件).

I want that user can choose folder (not file).

推荐答案

我认为我已经找到解决方法,但是弹出的内容是空的:

i think i have found solution but i have empty pop up :

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

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):
    pass

import kivy

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class MyApp(App):

    def build(self):
        layout = BoxLayout(orientation='vertical')
        root=Root()
        root.show_load()
        layout.add_widget(root)
        return layout

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

为什么我没有弹出文件夹?

why i have not folder in pop up ?

这篇关于使用FileChooser选择文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:13