AttributeError: วัตถุ 'NoneType' ไม่มีแอตทริบิวต์ 'setDelegate_' ฉันจะแก้ไขได้อย่างไร

โดยพื้นฐานแล้วฉันค่อนข้างใหม่กับ 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, line 240, ใน LeafApp notifyMe(Hey You! take a break now !!, คุณควรปฏิบัติตามกฎ 20-20-20 เพื่อจับตาดู สุขภาพดี)

ไฟล์ /Users/oats/Downloads/LEAF 1.0/Source Code/LEAF 0.6 .py, บรรทัด 231 ใน notifyMe alert.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 ใน แจ้งเตือน usrnotifctr.setDelegate(ตนเอง)

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()
'''

ขอขอบคุณอีกครั้งสำหรับทุกคนที่ติดต่อเรา ฉันสับสนอย่างยิ่งและต้องการความช่วยเหลือ!


person Oats.KV    schedule 14.05.2021    source แหล่งที่มา
comment
ไม่แน่ใจว่าเหตุใดคุณจึงได้รับข้อผิดพลาดนั้น แต่ LeafApp ของคุณจะไม่มีวันทำงานเนื่องจากโค้ดของคุณเข้าสู่วงวนไม่สิ้นสุด (while True) ก่อนที่แอปจะสามารถเริ่มต้นได้   -  person John Anderson    schedule 14.05.2021
comment
เห็นไหม นั่นคือสิ่งที่ฉันคิด แต่ด้วยเหตุผลบางอย่าง ทำไมถ้าฉันเอาตัวจับเวลาออก มันก็ทำงานได้อย่างสมบูรณ์แบบ ฉัน-ฉันไม่รู้ด้วยซ้ำ   -  person Oats.KV    schedule 14.05.2021


คำตอบ (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
person John Anderson    schedule 14.05.2021
comment
เพิ่ม import threading - person John Anderson; 14.05.2021
comment
ข้อผิดพลาดใหม่! รหัสทำงานได้ในขณะนี้เนื่องจากการรีแฟคเตอร์ของคุณ (tsym) รหัสใช้งานได้แต่การแจ้งเตือนไม่ทำงานใช่ไหม ค่อนข้างติดอยู่กับสิ่งนี้ .. alert.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 ใน แจ้ง usrnotifctr.setDelegate_(self) AttributeError: วัตถุ 'NoneType' ไม่มีแอตทริบิวต์ 'setDelegate' [INFO ] [Base ] เริ่มลูปหลักของแอปพลิเคชัน - person Oats.KV; 14.05.2021
comment
เฮ้เพื่อน ฉันเปลี่ยนรหัสโดยพื้นฐานแล้วและมันก็ใช้งานได้ ตอนนี้ฉันแค่ต้องสร้างตัวจับเวลาโดยที่การแจ้งเตือนจะเล่นหลังจากผ่านไป 20 นาที แต่ตัวจับเวลา 20 นาทีจะเริ่มขึ้นเมื่อกดปุ่ม หวังว่าฉันจะคิดออก ขอบคุณอีกครั้งเพื่อน! - person Oats.KV; 15.05.2021
comment
คลาส LeafApp (MDApp): def show_theme_picker (ตนเอง): theme_dialog = MDThemePicker () theme_dialog.open () def แจ้งเตือน (ชื่อ, ข้อความ): os.system ( osascript -e 'แสดงการแจ้งเตือน {} ด้วยชื่อ {}' .format ( ข้อความ, ชื่อเรื่อง)) แจ้งเตือน (LEAF, เฮ้, พักสักยี่สิบนาทีแล้วเหรอ?) def build(self): screen = Builder.load_string(screen_helper) กลับหน้าจอ LeafApp().run() - person Oats.KV; 15.05.2021