【问题标题】:How to create multiple python file and import them in a single python file in kivy如何在kivy中创建多个python文件并将它们导入单个python文件
【发布时间】:2020-07-10 07:40:13
【问题描述】:

我正在用 kivy 开发一个游戏,它有不同的关卡,每个关卡都有自己的 python kivy 文件。我正在使用屏幕管理器从一个屏幕级别移动到另一个屏幕级别。问题是如何使用 kivy 导入包含 main.py 文件中每个级别的那些 python Kivy 文件。我没有使用 .kv 文件。

# level1.py

import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class Level1(FloatLayout):
    def __init__(self, **kwargs):
        super(Level1, self).__init__(**kwargs)

         
        self.btn1=Button(text='level 1', size_hint=(0.5, 0.5), 
        on_press=self.click_b1))
      
     self.add_widget(self.btn1)
        
             
           
        def click_b1(self, instance):
             pass
       
class Level1App(App):
    def build(self):
        return Level1()

if __name__ == '__main__':
    Level1App().run()
# level2.py

import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class Level2(FloatLayout):
    def __init__(self, **kwargs):
        super(Level2, self).__init__(**kwargs)

         
        self.btn1=Button(text='level 2', size_hint=(0.5, 0.5), 
        on_press=self.click_b1))
      
     self.add_widget(self.btn1)
        
             
           
        def click_b1(self, instance):
             pass
       
class Level2App(App):
    def build(self):
        return Level1()

if __name__ == '__main__':
    Level2App().run()

如何在具有屏幕管理器的 main.py 中导入 level1.py 和 level2.py(不带 .kv)

# main.py with screen manager 

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition


class ScreenManagement(ScreenManager):
    def __init__(self, **kwargs):
        super(ScreenManagement, self).__init__(**kwargs)


class Level1Window(Screen):
    def __init__(self, **kwargs):
        super(Level1Window, self).__init__(**kwargs)

# run level1.py file here
        

class level2Window(Screen):
        def __init__(self, **kwargs):
            super(Level2Window, self).__init__(**kwargs)
            
# run level2.py file here

class Application(App):
    def build(self):
        sm = ScreenManagement(transition=FadeTransition())
        sm.add_widget(Level1Window(name='#'))
        sm.add_widget(Level2Window(name='#'))
        return sm


if __name__ == "__main__":
    Application().run()

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您正在为您的主游戏运行 Application(App) 类。因此,我从不在屏幕下调用不同的 .py 文件。所以我建议你在下面编写你的游戏屏幕类。因为你需要它。例如,如果你需要玩家通过下一级后的分数,你可以发送你当前的分数。屏幕管理器有有用的功能:

    on_pre_enter(加载页面前)、on_enter(加载页面后)、on_leaveon_pre_leave

    所以你只需要给你的页面提供 nameid。当你想发送分数和其他值低于 on_pre_leaveon_leave 函数。并切换页面:

    self.manager.current = 'page2name'
    

    使用 on_enteron_pre_enter 函数从上一页获取值:

    score = self.manager.ids.page1id.score
    

    如果有不同的捷径,我希望学习,但我认为这是最好和最快的编码方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2021-05-17
      • 2014-01-21
      • 2021-08-23
      相关资源
      最近更新 更多