【问题标题】:Cant access instances from another instantiating the class无法从另一个实例化类访问实例
【发布时间】:2020-02-01 03:42:39
【问题描述】:

使用 Kivy Factory 我已经在 ApplyPage 类中实例化了我的 MainWindow 类,当我尝试从 @987654324 访问它时,我试图从初始化为 None 的 MainWindow 类中访问 self.placementtext 变量@ 它返回为None。我知道它返回为None,因为这就是我将它初始化为的,但我想知道任何可能的方法来访问它在def printtext(self) 函数中分配的原始值。任何有关如何执行此操作的想法都将不胜感激,这是我的代码。

class MainWindow(Screen):
    search_string1 = ObjectProperty(None)
    search_string2 = ObjectProperty(None)



    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.placementtext = None
        self.searchbutton = Button( pos_hint = {"center_x": 0.5, "center_y": 0.5}, size_hint= (0.2, 0.1), font_name = 'fonts/Qanelas-Heavy.otf', text= "Search", background_color = (0.082, 0.549, 0.984, 1.0),                            background_normal= '', font_size = 20)
        self.searchbutton.bind(on_release=self.searchpressed)
        self.add_widget(self.searchbutton)


    def searchpressed(self, instance):
        app = App.get_running_app()
        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()
                data = "\n" + "\n" + str(name.val()) + '\n' + str(description.val()) + "\n" + str(
                    location.val()) + '\n' + str(date.val()) + '\n' + '\n' + str(price.val())
                project_list_screen = self.manager.get_screen('project_list_screen')
                project_list_screen.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())])
                project_list_screen.project_list._trigger_reset_populate()



                app.root.current = "project_list_screen"


    def printtext(self):
       self.placementtext = self.project_list_screen.project_list.adapter.selection[0]

Factory.register('MainWindow', cls=MainWindow)


class ApplyPage(Screen):
    mainwindow = ObjectProperty(None)

    def __init__(self, **kwargs):
        self.mainwindow = kwargs.pop('mainwindow', 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.add_widget(self.no)

    def on_enter(self, *args):
        print(self.mainwindow.placementtext)

这是我在 kivy 文件中实例化类的方式

 ApplyPage:
        id: applyingpage
        name: "applyingpage"
        mainwindow: Factory.MainWindow()

任何有关如何访问 self.placementtext 原始值的帮助将不胜感激

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    一种惰性方法是为应用程序类提供一个属性,您可以通过主屏幕将信息输入该属性,然后在第二个屏幕上读取。

    class MyApp(App):
        placementtext = None
    

    所以每当您更改放置文本时(因为它不是属性,不能使用 on_placementtext 绑定)只需添加

    app.placementtext = self.placementtext
    

    您现在可以通过调用 app.placementtext 从第二个屏幕访问它。例如:

    def on_enter(self, *args):
        print(app.placementtext)
    

    另一种方法是使用缓存。首先你注册你的缓存

    Cache.register('windows')
    

    然后在实例化时添加主窗口屏幕

    Cache.add('windows','mainwindow',#putyourmainwindowobjecthere)
    

    现在您可以从任何地方调用它!例如:

    def on_enter(self,*args):
        print(Cache.get('windows','mainwindow').placementtext)
    

    确保先从 kivy 导入模块!

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多