'NoneType' object has no attribute error, when using Kivy Screens - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: 'NoneType' object has no attribute error, when using Kivy Screens (/thread-1928.html) |
'NoneType' object has no attribute error, when using Kivy Screens - chelovek - Feb-04-2017 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! 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() RE: 'NoneType' object has no attribute error, when using Kivy Screens - Yoriz - Feb-04-2017 on_dropfile does not return anything so by default it returns None. RE: 'NoneType' object has no attribute error, when using Kivy Screens - chelovek - Feb-05-2017 (Feb-04-2017, 10:11 PM)Yoriz Wrote: on_dropfile does not return anything so by default it returns None. https://github.com/kivy/kivy/blob/master/examples/miscellaneous/multiple_dropfile.py Thanks. One thing I'm confused about is, the functions seem to work in this code above, but when I modify things a bit (adding screens, but leaving the core drop functions intact) it stops working. RE: 'NoneType' object has no attribute error, when using Kivy Screens - chelovek - Feb-05-2017 I spoke too soon. I needed to set up the Screen Manager within the build method. Thanks for your help! |