簡體   English   中英

AttributeError: 'NoneType' object 沒有屬性 'setDelegate_' 我該如何解決這個問題?

[英]AttributeError: 'NoneType' object has no attribute 'setDelegate_' how can I fix this?

基本上我對 python 和 kivy 還是很陌生,並試圖創建一個計時器應用程序,它會在 20 分鍾后向您的眼睛發出通知 rest。 我用 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(最近一次調用最后):

class LeafApp(MDApp) 中的文件“/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6.py”,第 218 行:

文件“/Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6.py”,第 240 行,在 LeafApp notifyMe("嘿你,現在休息一下!!", "你應該遵循 20-20-20保持眼睛健康的規則”)

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

在通知 self._notify(

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

AttributeError: 'NoneType' object 沒有屬性 '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()
'''

再次感謝任何伸出援手的人。 我完全糊塗了,需要幫助!

嘗試對您的代碼進行輕微的重構,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM