How can I display the full path of file in KIVY?

193 views
Skip to first unread message

Yan Lam

unread,
Jul 25, 2019, 2:29:49 PM7/25/19
to Kivy users support

I'm new to kivy and I'm using FileChooserIconView to display the files. I am trying to display the current path of the file, however, it cannot show the full path of a file. It is possible to show the full path?


I have tried to set a root path, it can show the full path of a file. However, it will not show a “..” directory to go up to the root path. I want to show the ".." directory too.

This is my kivy code:


48 <Browse>:
49     title: 'Select the ouput directory'
50     size_hint: .8, .8 #the presentage size of the original window
51     auto_dismiss: False
52     FileChooserIconView:
53         path: '.'
54         id: fc2
55         filters: [root.is_dir]
56         on_selection:
57         BoxLayout:
58             size_hint_y: .1
59             orientation: 'horizontal'
60             GridLayout:
61                 size_hint_y: None
62                 height: 150
63                 rows: 2
64                 orientation: 'vertical'
65                 spacing:1.5
66                 BoxLayout:
67                     Label:
69                         text: "Path of directory: " + fc2.path
70                 BoxLayout:
71                     Button:
72                         text: 'Back'
73                         on_release: root.dismiss() #close the popup 
74                     Button:
75                         text: "Choose"
76                         on_release: 
77                             root.dismiss()

When I run this code and click the button "Browse", it will popup and show as follow: Path of directory: . and if I click "../", it can show the full path.


But I want to show the full path of the directory when I click the button "Browse".

Elliot Garbus

unread,
Jul 25, 2019, 4:34:36 PM7/25/19
to kivy-...@googlegroups.com
Fc2.selection[0] will show the full path and filename.


--
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/4184e7b9-662c-49f6-8747-6fc78205f081%40googlegroups.com.

Yan Lam

unread,
Jul 25, 2019, 4:46:30 PM7/25/19
to Kivy users support
when I changed to fc2.selection[0], it gets an error: list index out of range
I think it is because I didn't give the default file.
and also I only want to show the directory path. 
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Jul 25, 2019, 5:19:54 PM7/25/19
to kivy-...@googlegroups.com
Selection is only set when a file is selected.

To display only a portion of the path I like to use pathlib, from the Python standard library.
You can import it into KV

#: import pathlib pathlib
Then I test if selection has been set to display the filename.  You can do something similar, using path lib to give you the path.
text: 'File: ' + ('<None>' if not fc.selection else pathlib.Path(fc.selection[0]).name)





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/0b4cd1b7-a29c-42b1-9a4d-cfa81d733f36%40googlegroups.com.

Yan Lam

unread,
Jul 26, 2019, 9:34:52 AM7/26/19
to Kivy users support
When I use pathlib.Path(fc.selection[0]).name), it can show the full path when I click some folders, however, it just can show "." when the screen popup, 
For example, when I click the button, the screen popup, and show the path: "." 
52     FileChooserIconView:
53         path: '.'
When I click the folder, and the path will show /home/user/test
So how can I show the full path when the screen popup for the first time. Thank you!

Elliot Garbus

unread,
Jul 26, 2019, 10:53:23 AM7/26/19
to kivy-...@googlegroups.com

You can set path in filechoose to the initial path you want:

 

       path: ‘the/initial/path/you/want/’

 

You can use pathlib to get the absolute  current working directory or the home directory.

 

       path: str(pathlib. Path.cwd()) # the current working directory

OR

       path: str(pathlib.Path.home()) # the home directory

 

these calls return a Path object, so need to be converted to strings to be used as the filechooser path.

Pathlib objects make it very easy to create paths.

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/e8b331d9-dd8c-4cd8-ac31-6f025b19f326%40googlegroups.com.

 

Yan Lam

unread,
Jul 26, 2019, 11:02:21 AM7/26/19
to Kivy users support
Thank you very much!
Reply all
Reply to author
Forward
0 new messages