亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

初始化時無法訪問類錯誤代碼中的實例

初始化時無法訪問類錯誤代碼中的實例

動漫人物 2022-07-12 18:17:06
每次我嘗試初始化一個 kivy 屏幕時,即使我已經研究過,我總是會收到一個錯誤,這表明我以正確的方式進行操作。我想從 my 訪問實例,我 ProjectListScreen class將實例初始化為 None 但將屏幕初始化為 ApplyPage 不起作用,我嘗試使用 Factory 將類實例化到 myApplyPage class 中,但它仍然沒有返回實例的值。下面是我的代碼,之后是我嘗試初始化屏幕類時的回溯。我將不勝感激任何幫助。請我整天都在為此苦苦掙扎。class ProjectListScreen(Screen):    project_list = ObjectProperty(None)    def __init__(self, **kwargs):        super(ProjectListScreen, self).__init__(**kwargs)        self.thelocalId = None        self.placementtext = None    def on_pre_enter(self, *args):        placements = database.child("placements").get()        placementslist = placements.val()        placementslist.items()        for key, value in placementslist.items():            self.key = key            key_list = []            key_list.append(key)            for key in key_list:                name = database.child("placements").child(str(key)).child("placement name").get()                description = database.child("placements").child(str(key)).child("placement description").get()                location = database.child("placements").child(str(key)).child("placement location").get()                date = database.child("placements").child(str(key)).child("placement date").get()                price = database.child("placements").child(str(key)).child("placement price").get()                thelocalId = database.child("placements").child(str(key)).child("localId").get()                self.thelocalId = thelocalId.val()                self.project_list.adapter.data.extend([ "\n" + "\n" + str(name.val()) + '\n' + str(description.val()) + "\n" + str(location.val()) + '\n' + str(date.val()) + '\n' + '\n' + str(price.val())])    def gettext(self):        self.placementtext = self.project_list.adapter.selection[0].textclass ApplyPage(Screen):    projectlistscreen = ObjectProperty(None)
查看完整描述

2 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

我會從 .kv 文件中刪除那個東西


WindowManager:

    id: window manager

    ApplyPage:

        id: applyingpage

        name: "applyingpage"

    ProjectListScreen:

        id: project_list_screen

        name: "project_list_screen"

并從 Python 中刪除


wm = WindowManager()

wm.add_widget(ApplyPage(projectlistscreen=projectlistscreen))


sm = Builder.load_file("kivy.kv")

并從 ApplyPage 類中刪除它


def nopressed(self, instance):

        sm.current = "placements"

你只需要這樣:


class WindowManager(ScreenManager):

    def __init__(self, **kwargs):

        super(WindowManager, self).__init__(**kwargs)


class MyApp(App):

    ...


    def build(self):

        self.refresh_token_file = self.user_data_dir + self.refresh_token_file

        self.thefirebase = MyFireBase()


        # I think you will need these objects later, so better to do it accessable for the whole class

        self.sm = WindowManager()

        self.pls = ProjectListScreen()

        self.applypage = ApplyPage(self.pls)


        # then add that screens to screen manager (it's even more simple than to do it in .kv)

        self.sm.add_widget(self.pls)

        self.sm.add_widget(self.applypage)

        return self.sm


    # if you need a transition to another screen, you can create method here

    def toapplypage(self):

        self.sm.transition.direction = 'left'

        self.sm.current = "applyingpage"

您可以使用方法來更改其他類中的屏幕,但您需要將您在 build 方法中創建的對象發送到那里。所以調用這些方法就像:


# it's only an example!

self.applypage.nopressed(self.sm)

并且不要忘記在 .kv 文件中寫入 ProjectListScreen 類的名稱。


查看完整回答
反對 回復 2022-07-12
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

__init__()您的類的方法ApplyPage需要位置參數projectlistscreen。kv加載文件并ApplyPage:遇到 時,將調用該方法__init__()而不需要所需的projectlistscreen參數。我建議修改該__init__()方法以使projectlistscreenakwarg參數為:


def __init__(self, **kwargs):

    self.projectlistscreen = kwargs.pop('projectlistscreen', None)

    super(ApplyPage, self).__init__(**kwargs)

    self.yes = Button(text="Yes", font_size = 20, font_name= "fonts/Qanelas-Heavy.otf", background_color = (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint = {"x":0.1,"y":0.05}, size_hint= [0.2, 0.1])

    self.add_widget(self.yes)

    self.no = Button(text="No", font_size= 20, font_name= "fonts/Qanelas-Heavy.otf", background_color = (0.082, 0.549, 0.984, 1.0), background_normal= '', pos_hint = {"x":0.7, "y":0.05}, size_hint= [0.2, 0.1])

    self.no.bind(on_pressed=self.nopressed)

    self.add_widget(self.no)

另外,我從以前的問題中識別出您的代碼,并且您再次犯了我在之前的帖子中指出的相同錯誤。你的代碼:


projectlistscreen = ProjectListScreen()

wm = WindowManager()

wm.add_widget(ApplyPage(projectlistscreen=projectlistscreen))

正在創建 a ProjectListScreen、 aWindowManager和 an ApplyPage。這些都沒有實際使用。


編碼:


sm = Builder.load_file("kivy.kv")

構建與Widgets上面相同,但這些實際上是使用的,因為sm是由您的build()方法返回的。


查看完整回答
反對 回復 2022-07-12
  • 2 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號