Binding values outside class

50 views
Skip to first unread message

mic...@morganfalls.com

unread,
Jul 21, 2016, 4:43:40 PM7/21/16
to Kivy users support
I'm trying to create a simple app (and in the process learn a little more about Kivy) but I can't seem to find much information on this specific subject.

I want to, for example, let the user be able to select a folder in the file system (in this case OS X or Linux) and then use python commands to do other things to that folder.  My issue is that since File Chooser is in a popup I can't figure out how to bind the label on the main screen to show the user selected folder.
Additionally, I'm unclear how to set the users choice to a variable that can be used in the additional python commands.

Could someone please help with the two lines noted below?

Thanks so much!
Michael
import kivy

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.pagelayout import PageLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
import os

Builder.load_string("""

<MainScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 30

Button:
text: 'Choose Source'
on_release: root.load_source()

Label:
id: _selection
text: "name of chosen folder should appear here"
size_hint_y: .5
canvas.before:
Color:
rgb: .5,.5,.4
Rectangle:
pos: self.pos
size: self.size

Button:
text: 'GO'
on_release: root.GetFolderName()

<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"

FileChooserListView:
path: '/volumes'
dirselect: True
id: filechooser
filter: ['*.mov']
multiselect: True
on_selection: root.selected(filechooser.selection)

BoxLayout:
size_hint_y: None
height: 30

Button
text: "Cancel"
on_release: root.cancel()

Button
text: "Select"
on_release: print str(filechooser.selection[0])
###########################################The following line clearly does not work##################################
#on_release: MainScreen._selection.text = str(filechooser.selection[0])
on_release: root.cancel()
""")


class LoadDialog(FloatLayout):

cancel = ObjectProperty(None)
def selected(self, filename):
print "new folder selected"

class MainScreen(FloatLayout):
def dismiss_popup(self):
self._popup.dismiss()
def load_source(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Select Source Folder", 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()

################## I'd like to do a few python things with this but Can't figure out how to get variables from the dialog in Kivy to bind with this#########################
def GetFolderName(self, *args):
y = "name of chosen folder"
print y 




class FootageImport(App):
    def build(self):
        return MainScreen()

if __name__ == "__main__":
FootageImport().run()


ZenCODE

unread,
Jul 21, 2016, 5:48:47 PM7/21/16
to Kivy users support
See below, not ideal but at least it works (I Hope!)

https://gist.github.com/Zen-CODE/63749afae7faf70a0de419f9e5f76360

mic...@morganfalls.com

unread,
Jul 21, 2016, 6:05:14 PM7/21/16
to Kivy users support
It's fantastic.  Thanks!!

Michael

unread,
Jul 24, 2016, 6:22:02 PM7/24/16
to Kivy users support
Allright.  I spent some more time on this and although the above example works (and was extremely helpful) I still can't seem to understand the most direct route to binding between classes.

I've been through the docs and forums but it's still a little fuzzy.

Using this very simple example below could someone please show what the most basic KV way to bind between these two Widget labels would be.

Thanks!!! 


import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.pagelayout import PageLayout
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.lang import Builder
import os

Builder.load_string("""


<MainScreen>:
id:Mainscreen
FloatLayout:
id:Float1
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
spacing: 0
Button:
text: 'Choose Source'
on_release: root.load_source()
size_hint_y: 1
Label:
id: _selection
size_hint_y: .5
canvas.before:
Color:
rgb: .5,.5,.4
Rectangle:
pos: self.pos
size: self.size

Label:                #<<<<<---------------------------------------------------HERE!
id: _Log
text: "I'D LIKE THE POPUP TEXT TO BE BOUND TO THIS (ID:_Log2)"


<LoadDialog>:
##Works

BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 30
Label:
id: _Log2                #<<<<<-------------------------------------------------HERE!
text: "I'D LIKE THIS TO BE BOUND TO THE MAIN WINDOW TEXT (ID:_Log)"

""")



#########################################################
#This defines the python code of the above widget.
class LoadDialog(FloatLayout):
selection = None
cancel = ObjectProperty(None)


class MainScreen(FloatLayout):

def dismissSource_popup(self):
self._popup.dismiss()
if self.content.selection is not None:
self.ids._selection.text = str(self.content.selection[0])
def load_source(self):
self.content = LoadDialog(load=self.load, cancel=self.dismissSource_popup)
self._popup = Popup(title="Select a Source Folder or file:",
content=self.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()



class SimpleBind(App):
def build(self):
return MainScreen()

if __name__ == "__main__":
SimpleBind().run()

Michael

unread,
Jul 25, 2016, 5:28:59 PM7/25/16
to Kivy users support
Alright I've managed to solve this using ScreenManager to pipe information to the various windows.  I'm assuming this is the general solution to this.


Thanks!
Reply all
Reply to author
Forward
0 new messages