How to dismiss a pop from a different screen

58 views
Skip to first unread message

Shoumik Das

unread,
Jul 16, 2020, 2:43:13 PM7/16/20
to Kivy users support
Hi. I have a situation where a file chooser is displayed in a pop-up window when a button is clicked inside a screen. After the file is selected, I would like to dismiss the popup window and/or updated the selected file path depending upon whether the user presses the 'Cancel' button or the 'Done' button. I am facing difficulty in calling the pop-up and dismiss functions from two different classes.

Python

class FileSelector(BoxLayout):
def selectfile(self, *args):
self.file_label.text = args[1][0]


class NewStatusScreen(Screen):
def back_to_status(self):
App.get_running_app().root.current='status_screen'
App.get_running_app().root.transition.direction='right'

def load_timezones(self):
App.get_running_app().root.get_screen('newstatus_screen').ids.dropdown4.clear_widgets()
f=open("tz-list","r")
for x in f:
App.get_running_app().root.get_screen('newstatus_screen').ids.dropdown4.add_widget(ImageLabelButtonTZ(source="images/globe-96.png",text=x))
f.close()

def launch_fileselector(self):
print("File selector launched.")
popup_selector=Popup(title='Select file',content=FileSelector(),size_hint=(0.9,0.9),auto_dismiss=False)
popup_selector.open()

def dismiss_fileselector(self):
print("File selector dismissed.")

Kivy

<FileSelector>:
orientation: 'vertical'
FileChooserListView:
on_selection:
root.selectfile(*args)
print("args: ", *args)
size_hint_y: 0.7
Label:
id: file_label
size_hint_y: 0.15
text: 'No file selected.'
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
Button:
text: 'Cancel'
# on_release: root.dismiss_fileselector()
Label:
id: fileselect_spacer
Button:
text: 'Done'


<NewStatusScreen>:
dropdown4: dropdown4.__self__
name: 'newstatus_screen'
canvas.before:
Color:
rgba: 255/255, 255/255, 255/255, 1
Rectangle:
pos: self.pos
size: self.size
AnchorLayout:
id: newstatus_backbtn_layout
anchor_x: 'left'
anchor_y: 'top'
ImageButton:
id: newstatus_back_button
size_hint: 0.1, 0.1
source: {'normal': 'images/back-96.png', 'down': 'images/back-96.png'} [self.state]
on_release: root.back_to_status()
BoxLayout:
id: newstatus_layout
orientation: 'vertical'
size_hint: 0.8, 0.9
pos_hint: {'center_x':0.5, 'center_y':0.5}
spacing: 2
BoxLayout:
id: btns_layout_row1
orientation: 'vertical'
CustomTextInput:
id: status_text
size_hint_y: 0.8
multiline: True
focus: True
hint_text: 'Enter your new status message'
font_size: '20sp'
max_characters: 280
Label:
id: chars_left
size_hint_y: 0.2
markup: True
text: str(len(status_text.text))+"/280"
color: 0/255, 0/255, 0/255, 1
text_size: self.size
halign: 'right'
valign: 'top'
BoxLayout:
id: id: btns_layout_row2
ImageLabelButtonTop:
id: parent_button4
source: 'images/globe-96.png'
text: 'UTC'
on_release:
root.load_timezones()
dropdown4.open(self)
on_parent: dropdown4.dismiss()
size_hint_y: None
height: '96dp'
DropDown:
id: dropdown4
on_select:
parent_button4.text = args[1][0]
parent_button4.source = args[1][1]
BoxLayout:
id: btns_layout_row3
orientation: 'horizontal'
spacing: 5
DatePicker:
pHint: 0.9, 0.8
BoxLayout:
spacing: 5
orientation: 'vertical'
canvas.after:
Color:
rgba: 128/255, 128/255, 128/255, 1
Line:
width: dp(1)
rectangle: (*self.pos, self.width, self.height)
Label:
id: time_label
text: 'TIME'
font_size: '11sp'
font_name: 'roboto/Roboto-Medium.ttf'
markup: True
halign: 'center'
valign: 'middle'
color: 0, 0, 0, 1
Slider:
id: hrs_slider
min: 0
max: 23
step: 1
cursor_size: 25, 25
value: 18
value_track: True
value_track_color: 254/255, 107/255, 100/255, 1
orientation: 'horizontal'
Label:
id: hrs_label
text:'HH: '+str(int(hrs_slider.value))
markup: True
font_size: '11sp'
font_name: 'roboto/Roboto-Medium.ttf'
color: 0, 0, 0, 1
halign: 'center'
valign: 'middle'
Slider:
id: mins_slider
min: 0
max: 59
step: 1
cursor_size: 25, 25
value: 30
value_track: True
value_track_color: 254/255, 107/255, 100/255, 1
orientation: 'horizontal'
Label:
id: mins_label
text:'MM: '+str(int(mins_slider.value))
markup: True
font_size: '11sp'
font_name: 'roboto/Roboto-Medium.ttf'
color: 0, 0, 0, 1
halign: 'center'
valign: 'middle'
BoxLayout:
id: btns_layout_row4
orientation: 'horizontal'
ImageButton:
id: gallery_button
source: {'normal': 'images/picture-96.png', 'down': 'images/picture-green-96.png'} [self.state]
on_release: root.launch_fileselector()
ImageButton:
id: savestatus_button
source: {'normal': 'images/save-96.png', 'down': 'images/save-close-96.png'} [self.state]

Can you please advise as to what should the correct approach for this function?

Thanks

Elliot Garbus

unread,
Jul 16, 2020, 2:58:47 PM7/16/20
to kivy-...@googlegroups.com

Make your FileSelector class a PopUp.  This way the cancel button can call root.dismiss()

 

See: https://github.com/ElliotGarbus/KivyFileAndDrive

Look at fctest.kv and classdefs.py

--
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/64240c59-39bb-4adc-ad72-dadea0f8dad0o%40googlegroups.com.

 

Shoumik Das

unread,
Jul 17, 2020, 1:00:36 PM7/17/20
to Kivy users support
Hi Elliot. That really helped. I followed your instructions and made the file selector work. However, I wanted to display the selected image file in the button and I used a StringProperty variable (selected_path) to hold the selected path but it didn't work. It seems I am not able to access the updated value of this variable (selected_path) from inside the kv file. Following are the changes I made:

Python

class FileSelector(Popup):
selected_path=StringProperty('')
def select_file(self, *args):
self.ids.file_label.text = args[1][0]
selected_path=args[1][0]
print("Selected path:",selected_path)
print("self:",self,"args:",args[1][0]) # self- FileSelector object, args - file path



class NewStatusScreen(Screen):
def back_to_status(self):
App.get_running_app().root.current='status_screen'
App.get_running_app().root.transition.direction='right'

def load_timezones(self):
App.get_running_app().root.get_screen('newstatus_screen').ids.dropdown4.clear_widgets()
f=open("tz-list","r")
for x in f:
App.get_running_app().root.get_screen('newstatus_screen').ids.dropdown4.add_widget(ImageLabelButtonTZ(source="images/globe-96.png",text=x))
f.close()

def launch_fileselector(self):
print("File selector launched.", self)
popup_selector=FileSelector(title='Select file')
popup_selector.open()

Kivy

<FileSelector>:
auto_dismiss: True
size_hint: 0.9, 0.9
BoxLayout:
orientation: 'vertical'
FileChooserListView:
size_hint_y: 0.7
on_selection:
root.select_file(*args)
# print("args: ", *args)
Label:
size_hint_y: 0.15
id: file_label
text: 'No file selected.'
BoxLayout:
size_hint_y: 0.15
orientation: 'horizontal'
Button:
text: 'Cancel'
on_release:
selected_path: ''
# print(selected_path) - Not defined error. 'root.selected_path' returns a null string.
# print(root, self, *args) # root - FileSelector object, self - Button object, args - Button object
root.dismiss()
Label:
id: fileselect_spacer
Button:
text: 'Done'
on_release:
print("KV Selected path:", root.selected_path) # returns a null string.
#app.root.get_screen('newstatus_screen').ids.gallery_button.source=root.selected_path
# print("Args:",args[0]) # args - Button object
root.dismiss()

Can you kindly advise on how I can access the value of this StringProperty variable from inside the kv file and assign it to the source value of the ImageButton?





The 'Cancel' button works fine. If I click on the 'Done' button, this is what I get:



Thanks in advance

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Jul 17, 2020, 5:33:24 PM7/17/20
to kivy-...@googlegroups.com

It is hard to know if this is the only issue.  But here is one problem:

 

               BoxLayout:

                  size_hint_y: 0.15

                  orientation: 'horizontal'

                  Button:

                        text: 'Cancel'

                        on_release:

                              selected_path: ''

                                                                           # print(selected_path) - Not defined error. 'root.selected_path' returns a null string.

                              # print(root, self, *args) # root - FileSelector object, self - Button object, args - Button

 

You are in the /on_release:’ expression.  I’m surprised this did not throw a parsing error.  It looks like you are creating a new kivy property ‘selected_path’ Perhaps you wanted:

               BoxLayout:

                  size_hint_y: 0.15

                  orientation: 'horizontal'

                  Button:

                        text: 'Cancel'

                        on_release:

                              root.selected_path = ''

                                                                           # print(selected_path) - Not defined error. 'root.selected_path' returns a null string.

                              # print(root, self, *args) # root - FileSelector object, self - Button object, args - Button

 

If you need more help, please share a runnable example.

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/8c5ca952-ce91-4f33-8b91-97692a9861a5o%40googlegroups.com.

 

Shoumik Das

unread,
Jul 18, 2020, 2:19:55 AM7/18/20
to Kivy users support
Hi Elliot. I am sharing a runnable example here along with the art. I am getting errors in a couple of situations. Can you please take a look? (Please refer attachment)

Click on this button to launch the file selector widget.


Scenario 1

Browse and select any image file. The path gets displayed correctly in the label below.



If I choose a different file in the same folder, the path gets updated correctly. However, if I try to change the directory to select a different file, I get the following error and the app crashes:



Scenario 2

After selecting an image file, click the 'Done' button. I would like the selected image to be set as the image icon of the file selector launch button (picture icon in the original screen). However, inspite of defining a StringProperty (selected_path), the latest variable value is not accessible from inside the kv file. I have used a few print functions to trace the variable calls. You can see that the output of 'KV Selected path:' is null. I was expecting the path value of the selected image to be printed here.



Thanks as always for your help and guidance.
filechooser.zip

Elliot Garbus

unread,
Jul 18, 2020, 12:04:45 PM7/18/20
to kivy-...@googlegroups.com

There were a number of minor issues – the major source of the problems was you were not using self to put data in the property, creating a new local variable.  I made changes to the python file and the kv files attached.  I’ve tried to put comments where I made changes.

 

 

class FileSelector(Popup):
   selected_path = StringProperty(
'')

  
def select_file(self, *args):
     
self.ids.file_label.text = args[1][0]
     
self.selected_path=args[1][0]              # need to access self.selected path
     
print("Selected path:",self.selected_path)
     
print("self:",self,"args:",args[1][0]) # self- FileSelector object, args - file path

The 'Cancel' button works fine. If I click on the 'Done' button, this is what I get:

 

 

Thanks in advance

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/fb8d14c8-ac04-47f7-8512-b3c7fe5347f3o%40googlegroups.com.

 

main.zip

Shoumik Das

unread,
Jul 18, 2020, 2:32:39 PM7/18/20
to Kivy users support
Thank you for helping me fix the issue. It was such a bad mistake. The selected path is working fine now. I should have used self since the path variable was defined inside the class.

For scenario 1 (IndexError: list index out of range error encountered while changing directories), I added the following check condition to verify that args was not empty on each click/select function call. The change directory activity does not have any returnable args value as no path is selected. This fixed the error.

class FileSelector(Popup):
selected_path = StringProperty('images/picture-96.png')

def select_file(self, *args):
if args[1]: # Check to see if args[1] is null. When changing directories, args does not contain any value. Returns null. Hence, the below variable assignments shall fail without the check to verify that args[1] actually holds a path value.
self.ids.file_label.text = args[1][0]
self.selected_path=args[1][0]              # need to access self.selected path
print("Selected path:",self.selected_path)
# print("self:",self,"args:",args[1][0]) # self- FileSelector object, args - file path. When changing directories, args[1] is null. Hence, the print statement returns an 'IndexError: list index out of range' error.

Thanks for the advice, Elliot.

Elliot Garbus

unread,
Jul 18, 2020, 2:59:21 PM7/18/20
to kivy-...@googlegroups.com
Glad to hear you got it working. 

Sent from my iPhone

On Jul 18, 2020, at 11:32 AM, Shoumik Das <shoum...@gmail.com> wrote:


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/9bf0b805-90ef-4272-897e-0b6a0d41493eo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages