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 類的名稱。

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()方法返回的。
添加回答
舉報