问题
i want to add kivy filechooser into gridlayout https://kivy.org/docs/api-kivy.uix.filechooser.html
i have my main class :
class MainApp(GridLayout):
def __init__(self, **kwargs):
mylayout = BoxLayout(orientation='vertical')
i want to add editor class in filechooser into mylayout BoxLayout if i add
mylayout.add_widget(Editor.run())
I have filechooser in fullscreen of my window, not in boxlayout
I want that user can choose folder (not file).
回答1:
Solution
Add dirselect: True
into FileChooserListView / FileChooserIconView
FileChooserListView:
id: filechooserlistview
dirselect: True
path:
filechooserlistview.path
FileChooser Controller » dirselect
dirselect
Determines whether directories are valid selections or not.
dirselect is a BooleanProperty and defaults to False.
回答2:
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 ?
来源:https://stackoverflow.com/questions/51651253/choose-folder-with-filechooser