У меня есть всплывающее окно, в котором я хочу выбрать файл и передать выбранный путь другой функции. Я хочу иметь как FileChooserListView, так и FileChooserIconView рядом друг с другом, а также метку сверху, где я отображаю текущий выбранный путь. Я создал Popup и FileChoosers со стороны python. Я добился наличия метки обновления, а также обновления FileChooserIconView, но обновление выбора FileChooserListView не работает при выборе файлов из FileChooserIconView. Кроме того, по какой-то причине мой выбор в FileChooserIconView обновляется только после однократного изменения каталога.
Прошу прощения за мои соглашения об именах, я знаю, что это ужасно.
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.filechooser import FileChooserIconView, FileChooserListView
import os
class BoxL(BoxLayout):
def __init__(self):
super(BoxL, self).__init__()
def chooseFilePopup(self, modus):
"""Create a Popup to chose a file for picture-screen. No Arguments/Returns."""
#create popup
self.chosePop = Popup()
self.chosePop.title = '[b]Choose picture file to display[/b]'
self.chosePop.title_color = 1, 1, 1, 1
self.chosePop.title_size = 20
self.chosePop.title_align = 'center'
self.chosePop.auto_dismiss = False
self.chosePop.size_hint = (None, None)
#create popup-content
choseBox = BoxLayout()
choseBox.orientation = 'vertical'
choseBox.spacing = 5
choseBox.padding = 30, 0, 30, 20
choseBoxLabel = Label()
if modus == 'local':
choseBoxLabel.text = '[b]Choose Local File:[/b] Navigate to file and doubleclick or press Load File'
if modus == 'online':
choseBoxLabel.text = '[b]Choose Online File:[/b] Copy and Paste URL and press Enter or Load File'
choseBoxLabel.size_hint_y = 0.2
choseBoxLabel.font_size = '20'
choseBoxLabel.color = 1, 1, 1, 1
choseBox.add_widget(choseBoxLabel)
if modus == 'local':
self.chosePop.size = (800, 600)
self.fichoo = FileChooserIconView()
self.fichoo.dirselect = True
self.fichoo.get_nice_size('fn') #this executed once allowed that the paths are displayed nicely
self.fichoo.path = "."
self.fichoo.entry_released = lambda *args: print_path(self.fichoo.selection, 'iconView') #on selection
self.fichooTwo = FileChooserListView()
self.fichooTwo.dirselect = True
self.fichooTwo.get_nice_size('fn')
self.fichooTwo.path = "."
self.fichooTwo.entry_released = lambda *args: print_path(self.fichooTwo.selection, 'listView')
fichooPath = Label()
fichooPath.size_hint_y = 0.1
fichooPath.text = 'Current path: ' + str(os.getcwd())
fichooPath.font_size = '20'
fichooPath.color = 1, 1, 1, 1
fichooPath.halign = 'left'
fileChooserBox = BoxLayout()
fileChooserBox.add_widget(self.fichoo)
fileChooserBox.add_widget(self.fichooTwo)
choseBox.add_widget(fichooPath)
choseBox.add_widget(fileChooserBox)
if modus == 'online':
self.chosePop.size = (640, 240)
fetchInput = TextInput()
fetchInput.on_text_validate = lambda *args: fetchMe(fetchInput.text)
choseBox.add_widget(fetchInput)
choseBoxInner = BoxLayout()
choseBoxInner.orientation = 'horizontal'
choseBoxInner.spacing = 20
choseBoxInner.size_hint_y = 0.2
choseButtonLoad = PopupButton()
choseButtonLoad.text = 'Load File'
if modus == 'local':
choseBoxInner.size_hint_y = 0.15
choseButtonLoad.on_release = lambda *args: loadMe(self.fichoo.selection)
if modus == 'online':
choseBox.spacing = 20
choseBox.padding = 30, 20, 30, 20
choseBoxInner.size_hint_y = 0.5
choseButtonLoad.on_release = lambda *args: fetchMe(fetchInput.text)
choseButtonCancel = PopupButton()
choseButtonCancel.text = 'Cancel'
choseButtonCancel.on_release = lambda *args: self.chosePop.dismiss()
choseBox.add_widget(choseBoxInner)
choseBoxInner.add_widget(choseButtonCancel)
choseBoxInner.add_widget(choseButtonLoad)
#inner functions
def fetchMe(url):
"""Create Loading Popup, Fetch picture from Web, start thread to generate_texture."""
self.chosePop.dismiss()
#Doing some stuff here, deleted for simplicity
def loadMe(selection):
"""Load filechooser selection, create Loading Popup, start thread to generate_texture."""
self.loadImageFile=str(selection[0])
#Doing some stuff here, deleted for simplicity
def print_path(selection, source):
"""Print current selected filechooser-path to label."""
#choseButtonLoad.text = 'Load File\n' + str(fichoo.selection[0])
if source == 'iconView':
self.fichooTwo.path = self.fichoo.path
self.fichooTwo.selection = selection
if source == 'listView':
self.fichoo.path = self.fichooTwo.path
self.fichoo.selection = selection
try:
fichooPath.text = 'Current path: ' + str(selection[0])
except:
pass
#add content, open popup
self.chosePop.content = choseBox
if modus == 'online':
fetchInput.focus = True
self.chosePop.open()
Я называю это из .kv с кнопкой на:
on_release: root.chooseFilePopup('local')