Python 无法从实例化该类的另一个实例访问实例
使用Kivy Factory,我在Python 无法从实例化该类的另一个实例访问实例,python,kivy,Python,Kivy,使用Kivy Factory,我在ApplyPage类中实例化了我的MainWindow类,我试图从初始化为None的MainWindow类访问self.placementtext变量,当我试图从ApplyPage访问它时,它返回为None。我知道它返回为None,因为这就是我初始化它的原因,但我想知道任何可能的方法来访问它在def printtext(self)函数中指定的原始值。任何关于如何做到这一点的想法都将不胜感激,这是我的代码 class MainWindow(Screen):
ApplyPage
类中实例化了我的MainWindow
类,我试图从初始化为None的MainWindow类访问self.placementtext
变量,当我试图从ApplyPage
访问它时,它返回为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
原始值的帮助都将不胜感激一种惰性方法是为应用程序类提供一个属性,您可以在主屏幕上输入信息,然后在第二屏幕上读取
class MyApp(App):
placementtext = None
因此,每当您更改放置文本时(因为它不是一个属性,无法在\u 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导入模块
希望这有帮助