【发布时间】: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 原始值的帮助将不胜感激
【问题讨论】: