【问题标题】:AttributeError: 'NoneType' object has no attribute 'setDelegate_' how can I fix this?AttributeError: 'NoneType' 对象没有属性 'setDelegate_' 我该如何解决这个问题?
【发布时间】:2021-05-14 07:35:05
【问题描述】:

基本上,我对 python 和 kivy 还很陌生,我正在尝试创建一个计时器应用程序,它会在 20 分钟后发出通知让你的眼睛休息。我用 plyer 来设置通知,我在 mac 上。通常由于 pyobjus 错误,通知不起作用,但我解决了这个问题。但现在我留下了这个新错误。

runfile('/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6 .py', wdir='/Users/oats/Downloads/LEAF 1.0/Source Code') Traceback (大部分 最近通话最后一次):

文件“/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6 .py”,行 218,在 类 LeafApp(MDApp):

文件“/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6 .py”,行 240,在 LeafApp notifyMe("Hey You!休息一下!!", "你应该遵守 20-20-20 规则,保持眼睛健康")

文件“/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6 .py”,行 231,在通知我 通知.通知(

文件 “/Users/oats/opt/anaconda3/lib/python3.8/site-packages/plyer/facades/notification.py”, 第 79 行,在通知中 self._notify(

文件 “/Users/oats/opt/anaconda3/lib/python3.8/site-packages/plyer/platforms/macosx/notification.py”, 第 38 行,在 通知中 usrnotifctr.setDelegate(self)

AttributeError: 'NoneType' 对象没有属性 'setDelegate_'

是完整的错误消息。我的代码如下。

'''

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.picker import MDDatePicker
from kivymd.uix.picker import MDThemePicker
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import MDList
from kivy.properties import ObjectProperty
from kivymd.uix.navigationdrawer import MDNavigationDrawer
from kivy.properties import StringProperty, ListProperty
from kivy.lang import Builder
from kivy.core.window import Window
from plyer import notification
import time
import plyer


Config.set('graphics', 'width', '1600')
Config.set('graphics', 'height', '1200')


KV = '''
<Box@BoxLayout>:
    bg: .65, .48, .35, 1 
       
             

BoxLayout:
    Rectangle:    
        size: 1600, 1200

    Box:
        bg: app.theme_cls.bg_light
    Box:
        bg: app.theme_cls.bg_normal
    Box:
        bg: app.theme_cls.bg_dark
    Box:
        bg: app.theme_cls.bg_darkest
            
'''
            
screen_helper = """
ScreenManager:
    MenuScreen:
    HomeScreen:
    BreakScreen:
    SettingsScreen
        
<MenuScreen>:
    name: 'menu'
    
  
        
    MDLabel:
        rectangle:
        background_color: .65, .48, .35, 1           
        size: 300, 700
        pos_hint: {'center_x':0.1, 'center_y':0.0}
        
                
    MDLabel:
        text: "Welcome to"
        font_style: 'H2'
        size:500, 500
        pos_hint: {'center_x':0.95,'center_y':0.84}
        
    MDLabel:
        text: "LEAF"
        font_style: 'H3'
        size:500, 500
        pos_hint: {'center_x':1.045,'center_y':0.7}
        
        
    MDFlatButton:
        text: 'Calendar'
        font_style: 'H6'
        pos_hint: {'center_x':0.091,'center_y':0.65}
        on_release: app.show_date_picker()
            
            
    MDFlatButton:
        text: 'Home'
        font_style: 'H6'
        pos_hint: {'center_x':0.076,'center_y':0.57}
        on_press: root.manager.current = 'home'
        
    MDFlatButton:
        text: 'Settings'
        font_style: 'H6'
        pos_hint: {'center_x':0.09,'center_y':0.49}
        on_press: root.manager.current = 'settings'
        
            
    MDRectangleFlatButton:
        text: 'Continue'
        font_style: 'H6'
        pos_hint: {'center_x':0.61,'center_y':0.345}
        on_press: root.manager.current = 'home'
   


    
<HomeScreen>:
    name: 'home'    
                
        
    MDToolbar:
        id: toolbar
        title: "Home
        pos_hint: {"top": 1}
        elevation: 5
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
        
    Widget:

        MDNavigationDrawer:
            id: nav_drawer
    
            ContentNavigationDrawer:
                orientation: 'vertical'
                padding: "8dp"
                spacing: "8dp"
                

                MDFlatButton:
                    text: 'Return to menu'
                    font_style: 'Subtitle1'
                    on_release: root.manager.current = 'menu'

                MDFlatButton:
                    text: 'Open Calendar'
                    font_style: 'Subtitle1'
                    on_release: app.show_date_picker()
                    
                MDFlatButton:
                    text: 'Open Settings'
                    font_style: 'Subtitle1'
                    on_press: root.manager.current = 'settings'



    
                    
                    
                            
                
                
            
    
<BreakScreen>:
    name: 'break'
    
<SettingsScreen>:
    name: 'settings'
    
    
    MDFlatButton:
        text: 'Return to menu'
        font_style: 'H6'
        pos_hint: {'center_x':0.1,'center_y':0.05}
        on_press: root.manager.current = 'menu'
        
    MDFlatButton:
        text: 'Change Theme'
        font_style: 'H6'
        pos_hint: {'center_x':0.85,'center_y':0.95}
        on_release: app.show_theme_picker() 
        
    MDFloatingActionButton:
        icon: 'moon-waning-crescent'
        theme_text_color: "Custom"
        md_bg_color: 0, 0.039, 0.867, 0.557
        pos_hint: {'center_x':0.9,'center_y':0.3}
        on_press: self.theme_cls.theme_style = "Dark"  # "Light"
        
    MDFloatingActionButton:
        icon: 'MDFloatingActionButton'
        icon: 'lightbulb'
        pos_hint: {'center_x':0.9,'center_y':0.1}
        on_press: self.theme_cls.theme_style = "Light"  # "Dark"
        
        
"""  

class MenuScreen(Screen):
    pass


class HomeScreen(Screen):
    pass


class BreakScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

class ContentNavigationDrawer(BoxLayout):
    pass

class DrawerList(ThemableBehavior, MDList):
    pass


    
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(HomeScreen(name='profile'))
sm.add_widget(BreakScreen(name='upload'))
sm.add_widget(SettingsScreen(name='settings'))



class LeafApp(MDApp):
    
    def show_date_picker(self):
        date_dialog = MDDatePicker()
        date_dialog.open()

    def show_theme_picker(self):
        theme_dialog = MDThemePicker()
        theme_dialog.open()



    def notifyMe(ttle, msg):
        notification.notify(
            title = ttle,
            message = msg,
            timeout = 10,
    )


    if __name__ == '__main__':
        while True:
            notifyMe("Hey You! take a break now !!", "You should follow the 20-20-20 rule to keep your eyes healthy")
            time.sleep(1200)
            



    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen
    
LeafApp().run()
'''

再次感谢任何伸出援手的人。我完全糊涂了,需要帮助!

【问题讨论】:

  • 不确定为什么会出现该错误,但您的 LeafApp 将永远不会运行,因为您的代码在应用程序启动之前进入了无限循环 (while True)。
  • 看,这就是我的想法,但出于某种原因,我想知道为什么如果我拿出计时器的东西它会完美运行。我、我什至不知道

标签: python macos notifications kivy kivymd


【解决方案1】:

尝试对您的代码进行轻微重构,如下所示:

def notifyMe(self, ttle, msg):  # added "self" arg
    notification.notify(
        title=ttle,
        message=msg,
        timeout=10,
    )

def timer_loop(self):
    while True:
        self.notifyMe("Hey You! take a break now !!", "You should follow the 20-20-20 rule to keep your eyes healthy")
        time.sleep(1200)

def build(self):
    # run timer as a separate thread
    threading.Thread(target=self.timer_loop, daemon=True).start()

    screen = Builder.load_string(screen_helper)
    return screen

【讨论】:

  • 新错误!由于您的重构(tsym),代码现在运行。所以代码有效但通知无效?相当卡在这个..notification.notify(文件“/Users/oats/opt/anaconda3/lib/python3.8/site-packages/plyer/facades/notification.py”,第79行,通知self._notify(文件“/Users/oats/opt/anaconda3/lib/python3.8/site-packages/plyer/platforms/macosx/notification.py”,第 38 行,在 notify usrnotifctr.setDelegate_(self) AttributeError: 'NoneType'对象没有属性'setDelegate' [INFO] [Base] 启动应用程序主循环
  • 嘿伙计,我基本上完全改变了代码并且它可以工作。现在我只需要制作某种计时器,通知在二十分钟后播放,但二十分钟计时器在按下按钮时启动。希望我能弄清楚。谢谢你!
  • class LeafApp(MDApp): def show_theme_picker(self): theme_dialog = MDThemePicker() theme_dialog.open() def notify(title, text): os.system(""" osascript -e 'display通知 "{}" 标题为 "{}"' """.format(text, title)) notify("LEAF", "嘿,已经二十分钟了,休息一下?") def build(self): screen = Builder.load_string(screen_helper) 返回屏幕 LeafApp().run()
猜你喜欢
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多