Load and read a file from selection in filechooser

86 views
Skip to first unread message

NDD Mais

unread,
Aug 5, 2023, 1:55:05 PM8/5/23
to kivy-...@googlegroups.com
Hi
I'm trying to read an mp4 file with videoplayer, from selection, since the user, when he clicks on the open button, a boxdialog appears, giving the option to choose to read or something else, and after accepting to read, the player had to be able to read it. However, even satisfying the condition, which generates an error, does not work anyway. Can you help me
import os



os.environ["KIVY_VIDEO"] = "ffpyplayer"
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
import tkinter
from kivymd.theming import ThemeManager
import PIL
import tkinter.messagebox as mb
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDRectangleFlatButton, MDFlatButton
from kivymd.uix.screen import MDScreen
from tkinter.simpledialog import Dialog

from tkinter import filedialog
from tkinter import messagebox
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from tkPDFViewer import tkPDFViewer as pdf
from PIL import Image, ImageTk, Image
from tkPDFViewer import *
from tkinter import *
import tkinter as tk
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
from kivy.uix.actionbar import ActionItem
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen

Window.size = (800, 400)

Builder.load_string("""
<WindowManager>:
Screen1
Screen2
Screen3


<Screen1>:
name:'Firts'
title:'ab'
id:'my_dialog'

MDBoxLayout:
orientation:'vertical'
MDTopAppBar:
title:'Menu'

MDFloatingActionButtonSpeedDial:
root_button_anim:True
data:app.data
callback:app.callback



<Screen2>:
name:'Segundo'
id: my_widget
#:import Path pathlib.Path
# imported pathlib to use in kv
BoxLayout:
BoxLayout:
orientation: 'vertical'
VideoPlayer:
id: video_tape
allow_stretch: True
options: {"eos": "loop"}
options:{'fit_mode': 'contain'}
Button:
size_hint_y: None
height: 48

text: "oOOOpen"
disabled: not filechooser.selection # disable button in no selection has been made
on_release: root.opend() # path not used, selection is full path
BoxLayout:
orientation:'vertical'
FileChooserListView:
id: filechooser
path: str(Path().home() / 'Videos') # changed the default path for filechooser
# my_widget was fine, but easy to use root
on_selection: root.selected(filechooser.path,filechooser.selection)
""")


class WindowManager(ScreenManager):
pass


class Screen1(Screen):
pass


class Screen2(Screen):
theme_cls = ThemeManager()
def selected(self, filechooser,filename):
p=self.selected(filename)
return p



def opend(self, filechooser='',path=''):
self.show_alert_dialog_d()
dialog=None
def show_alert_dialog_d(self, filechooser=''):

if not self.dialog:
self.dialog = MDDialog(
text="Doyou like mbolo",
buttons=[
MDFlatButton(
text="Yes",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.o_dx
),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.close_dialog_d
),
],
)
self.dialog.open()
def close_dialog_d(self,obj):
self.dialog.dismiss()



def o_dx(self,filename,filechooser='',path=''):

op=self.selected(filename)
ops = "".join(map(str, op))

print(ops)
if ops.endswith('mp4'):
self.ids.video_tape.source = ops
self.ids.video_tape.state='play'
# return video_tape


class Screen3(Screen):
pass


class MyAppt(MDApp):
data = {
"File": "File",

}
def callback(self,obj):
self.show_alert_dialog()
def caut(self,obj):
self.close_dialog(obj)
self.root.current='Segundo'

dialog = None
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
text="mjjjjjjjjjjjjjjjjjjjjjjj",
buttons=[
MDFlatButton(
text="CANCEL",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.caut
),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.close_dialog
),
],
)
self.dialog.open()

def close_dialog(self,obj):
self.dialog.dismiss()


def build(self):
self.theme_cls.theme_style_switch_animation = 'blue'
self.theme_cls.theme_style = "Dark"

self.theme_cls.primary_palette = 'Gray'
self.data = {'File': [r'C:\VdProject\facebook.png', 'on_release', self.callback],
'Folder': r'C:\VdProject\facebook.png'}

return WindowManager()


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

NDD Mais

unread,
Aug 5, 2023, 1:55:54 PM8/5/23
to kivy-...@googlegroups.com

elli...@cox.net

unread,
Aug 5, 2023, 4:35:22 PM8/5/23
to kivy-...@googlegroups.com

Here is an example of a simple filechooser.  The point of this exercise was to add a drive selector.  This is a relatively simple example that does a load and save dialogs with the filechooser.

https://github.com/ElliotGarbus/KivyFileAndDrive

 

run main.py 

You can ignore main1.py,  it is a simple test of the popup using Factory.

 

 

The filter is set to only show *.txt or *.pdf files.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAFzdpLjuLAFm56kYjwuxKuuZ8y_VBtPTnSydoeAxhwSGdr7efg%40mail.gmail.com.

NDD Mais

unread,
Aug 6, 2023, 9:46:31 AM8/6/23
to kivy-...@googlegroups.com
Hi
Thanks for the answer. However, I think I didn't expose the subject well, from what I understood in your answer. The file loading and reading happened satisfactorily, due to a problem, it started when I put a Boxdialog with options 'Open' and 'others'...for other purposes to be considered. Between the open button and reading the selected file, the problem here is that it doesn't load the file to read in the reading function (o_dx), the variable in that function doesn't even receive the path. 
Regards

NDD Mais

unread,
Aug 6, 2023, 10:12:55 AM8/6/23
to kivy-...@googlegroups.com
Ok, your proposal solution, a little different from what i wanted, but give it a try, and see how it looks when inserted into the main program and its layouts

elli...@cox.net

unread,
Aug 6, 2023, 11:27:27 AM8/6/23
to kivy-...@googlegroups.com

Looking at this code:

 

class Screen2(Screen):

    theme_cls = ThemeManager()

 

    def selected(self, filechooser,filename):

        p=self.selected(filename)

        return p

 

You have a problem that selected is calling itself.  No need for recursion here. 

Also note that selection, filename here, is a list.  Print it out, you will see it a list at that the filename is an absolute filename with full path info.

You might want to change your kv code to:

 

on_selection: root.selected(filechooser.selection[0])

elli...@cox.net

unread,
Aug 6, 2023, 11:28:56 AM8/6/23
to kivy-...@googlegroups.com

I was not suggesting you use the code – or change the look.  It is just an example of a file chooser.

 

Sent: Sunday, August 6, 2023 7:12 AM
To: kivy-...@googlegroups.com

NDD Mais

unread,
Aug 6, 2023, 11:50:51 AM8/6/23
to kivy-...@googlegroups.com
Thanks, I will check it out.

Regards

NDD Mais

unread,
Aug 6, 2023, 1:05:37 PM8/6/23
to kivy-...@googlegroups.com
The problem here is that the select function gets the file path, but does not transmit it to the read function to be able to read it.
                on_release: root.show_alert_dialog_d()  # path not used, selection is full path
        BoxLayout:
orientation:'vertical'
FileChooserListView:
id: filechooser
path: str(Path().home() / 'Videos') # changed the default path for filechooser
# my_widget was fine, but easy to use root
                on_selection: root.selected(filechooser.path,filechooser.selection[0])
""")


class WindowManager(ScreenManager):
pass


class Screen1(Screen):
pass


class Screen2(Screen):
theme_cls = ThemeManager()

    def selected(self, filechooser, filename=''):
p=filename
print('lllllllllllllllllllll',p)

return p


def opend(self, filechooser='',path=''):
self.show_alert_dialog_d()
dialog=None
def show_alert_dialog_d(self, filechooser=''):

if not self.dialog:
self.dialog = MDDialog(
text="Doyou like mbolo",
buttons=[
MDFlatButton(
text="Yes",
theme_text_color="Custom",
                        text_color=self.theme_cls.primary_color,on_release=self.read

),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.close_dialog_d
),
],
)
self.dialog.open()
def close_dialog_d(self,obj):
self.dialog.dismiss()



    def read(self,filechooser='',filename=''):

op= self.selected(filechooser='')
print('ldldldldld',op)

ops = "".join(map(str, op))

print(ops)
if ops.endswith('mp4'):
self.ids.video_tape.source = ops
self.ids.video_tape.state='play'
# return video_tape


class Screen3(Screen):
pass


class MyAppt(MDApp):
data = {
"File": "File",

}
def callback(self,obj):
self.show_alert_dialog()
def caut(self,obj):
self.close_dialog(obj)
self.root.current='Segundo'

dialog = None
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
text="mjjjjjjjjjjjjjjjjjjjjjjj",
buttons=[
MDFlatButton(
                        text="YES",
                        theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.caut
),
MDFlatButton(
text="DISCARD",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,on_release=self.close_dialog
),
],
)
self.dialog.open()

def close_dialog(self,obj):
self.dialog.dismiss()


def build(self):
self.theme_cls.theme_style_switch_animation = 'blue'
self.theme_cls.theme_style = "Dark"

self.theme_cls.primary_palette = 'Gray'
self.data = {'File': [r'C:\VdProject\facebook.png', 'on_release', self.callback],
'Folder': r'C:\VdProject\facebook.png'}

return WindowManager()


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

NDD Mais

unread,
Aug 6, 2023, 1:06:24 PM8/6/23
to kivy-...@googlegroups.com

elli...@cox.net

unread,
Aug 6, 2023, 2:00:21 PM8/6/23
to kivy-...@googlegroups.com

You are calling selected when the file is selected.  Store the selected filename in an instance variable, let’s call that self.selected_file.  In your read method() use self.selected_file to read the file. 

elli...@cox.net

unread,
Aug 6, 2023, 2:02:48 PM8/6/23
to kivy-...@googlegroups.com

Alternatively, you could call read() and pass in the filename from kv.  The core of the problem is calling selected() from read(),

NDD Mais

unread,
Aug 6, 2023, 2:26:54 PM8/6/23
to kivy-...@googlegroups.com
Many thanks, it works.

Regards

Reply all
Reply to author
Forward
0 new messages