猿问

初始化时无法访问类错误代码中的实例

每次我尝试初始化一个 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].text


class ApplyPage(Screen):

    projectlistscreen = ObjectProperty(None)


动漫人物
浏览 81回答 2
2回答

倚天杖

我会从 .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 类的名称。

慕后森

__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()方法返回的。
随时随地看视频慕课网APP

相关分类

Python
我要回答