我编写了一个用于显示测量值的 python kivy 应用程序。在 kivy 上搜索资料时,我偶然发现了 kivy-md(kivy Material 设计)项目。我发现 UI 非常好看。这就是为什么我想将我的应用程序注入(inject)到 kivy-md 项目的屏幕中。
我的项目在文件夹 kivy-playground
中,其中包含文件夹 kivymd
中的 kivymd
文件,main.py
(用于启动 kivy md 应用程序)、main.kv
文件(用于 kivy md 应用程序的布局)和一个包含用于显示测量值的 kivy 应用程序。我正在使用 Python 3.7 和最新版本的 kivy。
Goal: I want to inject the Application view from
playground.py
into the main application which is started by themain.py
such that the view of theplayground.py
is displayed on the screenpage2
(seemain.kv
) of the main application. I am totally lost how this can be achieved and I would be happy if someone could show me on this toy example how this can be achieved. It is not necessarily important that theplayground.py
stays as it is. If the problem can be solved by small changes in theplayground.py
file then this would be also a valid solution.
我试图编写一个最小的工作示例。这是文件
# main.py
# -*- coding: utf-8 -*-
import os
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivymd.theming import ThemeManager
class MainApp(App):
theme_cls = ThemeManager()
def __init__(self, **kwargs):
super(MainApp, self).__init__(**kwargs)
Window.bind(on_close=self.on_stop)
def build(self):
main_widget = Builder.load_file(
os.path.join(os.path.dirname(__file__), "./main.kv")
)
self.theme_cls.theme_style = 'Dark'
return main_widget
def close_app(self, *largs):
super(MainApp, self).stop(*largs)
def on_pause(self):
return True
if __name__ == '__main__':
MainApp().run()
这是main.kv
# main.kv
#:kivy 1.10.1
#:import Toolbar kivymd.toolbar.Toolbar
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
id: nav_drawer
NavigationDrawerToolbar:
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: "Page1"
on_release: app.root.ids.scr_mngr.current = 'page1'
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: "Page2"
on_release: app.root.ids.scr_mngr.current = 'page2'
BoxLayout:
orientation: 'vertical'
halign: "center"
Toolbar:
id: toolbar
md_bg_color: app.theme_cls.primary_color
background_palette: 'Primary'
background_hue: '500'
right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: scr_mngr
Screen:
name: 'page1'
Label:
text: "page1"
Screen:
name: 'page2'
Label:
text: "Page 2"
这是我要注入(inject)到主应用程序屏幕 page2
中的 playground.py
。
from kivy.app import App
from kivy.uix.label import Label
class Playground(App):
def build(self):
hello_world_label = Label(text="Hello World!")
return hello_world_label
if __name__ == "__main__":
Playground().run()
最佳答案
如果您可以将 Playground.py
更改为如下内容:
from kivy.app import App
from kivy.uix.label import Label
def getPlaygroundRoot():
hello_world_label = Label(text="Hello World!")
return hello_world_label
class PlayGround(FloatLayout):
def __init__(self, **kwargs):
super(PlayGround, self).__init__(**kwargs)
self.add_widget(getPlaygroundRoot())
class Playground(App):
def build(self):
return getPlaygroundRoot()
if __name__ == "__main__":
Playground().run()
然后,在您的 main.py
中,您可以添加:
from playGround import getPlaygroundRoot
然后使用 add_widget(getPlaygroundRoot())
将 Playground
根添加到 MainApp
中的某个容器。
或者,如果你想在你的 .kv
文件中使用 Playground
,你可以添加
#:import playground playGround
到你的 .kv
文件,然后添加:
Screen:
name: 'page2'
Label:
text: "Page 2"
pos_hint: {'center_x': 0.5, 'y': 0.8}
size_hint: (1.0, 0.2)
PlayGround:
pos_hint: {'center_x': 0.5, 'y': 0.1}
size_hint: (1.0, 0.2)
将其添加到您的page2
。
关于Python Kivy 如何将用 Python 制作的小部件注入(inject) kv 文件中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259995/