I'm working on a multi-screen Kivy app. This is a simplified version, but the basic functionality is that a user should be able to drag files to three labels on the main screen.
I declare self.drops=[] in the App class, and in the label ("DropFile") class I have:
app = App.get_running_app()
app.drops.append(self.on_dropfile)
I was able to get the functionality to work without screens, but when I tried adding a screen manager, I started getting this error: 'NoneType' object has no attribute 'drops'.
Any ideas what's going wrong with "drops"? I've added comments to the code with the "PROBLEM AREAS". If you comment out drops, the app will open (albeit without drag/drop functionality).
Any help would be greatly appreciated!
I declare self.drops=[] in the App class, and in the label ("DropFile") class I have:
app = App.get_running_app()
app.drops.append(self.on_dropfile)
I was able to get the functionality to work without screens, but when I tried adding a screen manager, I started getting this error: 'NoneType' object has no attribute 'drops'.
Any ideas what's going wrong with "drops"? I've added comments to the code with the "PROBLEM AREAS". If you comment out drops, the app will open (albeit without drag/drop functionality).
Any help would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
from kivy.app import App from kivy.uix.button import Button from kivy.core.window import Window from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.listview import ListView from kivy.uix.gridlayout import GridLayout from kivy.properties import ObjectProperty from kivy.uix.screenmanager import ScreenManager, Screen from kivy.lang import Builder from kivy.properties import ListProperty Builder.load_string( """ <DropFile>: <RunButton>: color: .5,.6,.2,.7 <MenuScreen>: BoxLayout: orientation: 'vertical' DropFile: text: "Drag file1 here." DropFile: text: "Drag file2 here." DropFile: text: "Drag file3 here." RunButton: text: "Run" on_press: root.manager.current = "settings" <SettingsScreen>: BoxLayout: Button: text: 'My settings button' Button: text: 'Back to menu' on_press: root.manager.current = 'menu' """ ) # Declare both screens class MenuScreen(Screen): pass class Box(BoxLayout): pass class RunButton(Button): pass class Helper: pass class DropFile(Label): def __init__( self , * * kwargs): super (DropFile, self ).__init__( * * kwargs) Window.bind(mouse_pos = lambda w, p: setattr (Helper, 'mpos' , p)) #PROBLEM AREA 1: Something is malfunctioning with "drops" here app = App.get_running_app() app.drops.append( self .on_dropfile) def on_dropfile( self , widget, filename): # a function catching a dropped file # if it's dropped in the widget's area if self .collide_point( * Window.mouse_pos): self .text = filename.decode( 'utf-8' ) print ( self .text) class SettingsScreen(Screen): pass # Create the screen manager sm = ScreenManager() sm.add_widget(MenuScreen(name = 'menu' )) sm.add_widget(SettingsScreen(name = 'settings' )) class TestApp(App): def build( self ): #PROBLEM AREA 2: Something is malfunctioning with "drops" here. self .drops = [] Window.bind(on_dropfile = self .handledrops) return sm def handledrops( self , * args): for i in self .drops: i( * args) if __name__ = = '__main__' : TestApp().run() |