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