[英]Kivy Screenmanager Issue
I'm trying to create a mobile app with multiple screen (one of these with camara object embedded), using kivy, but the code below, produces only a black window, without showing nothing.我正在尝试使用 kivy 创建一个具有多个屏幕的移动应用程序(其中一个嵌入了 camara object),但下面的代码只产生黑色 window,没有显示任何内容Can you please help me figuring out why this isn't working?你能帮我弄清楚为什么这不起作用吗?
import kivy, ctypes
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.camera import Camera
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class camera(Screen):
def build(self):
#camera obj
self.camera_obj = Camera()
self.camera_obj.resolution = (800,800)
#button obj
button_go_back = Button(text = "go back")
button_go_back.size_hint = (.5,.2)
button_go_back.pos_hint = {"x": .25, "y": .25}
#layout
layout = BoxLayout()
layout.add_widget(self.camera_obj)
layout.add_widget(button_go_back)
return layout
class MainWindow(Screen):
def build(self):
button_take_picture = Button(text = "take a picture")
button_take_picture.size_hint = (.5,.2)
button_take_picture.pos_hint = {"x": .25, "y": .25}
layout = BoxLayout()
lab = Label(text="some text")
layout.add_widget(button_take_picture)
layout.add_widget(lab)
return layout
screen_manager=ScreenManager()
screen_manager.add_widget(MainWindow(name='main'))
screen_manager.add_widget(camera(name='camera'))
class NMS(App):
def build(self):
return screen_manager
if __name__ == "__main__":
ctypes.windll.shcore.SetProcessDpiAwareness(1)
NMS().run()
Since you are initializing the Screen
s in python you might need the __init__
constructor not build (as far as I'm aware there is no default method named build
for Screen
widget).由于您在 python 中初始化Screen
s,因此您可能需要不构建__init__
构造函数(据我所知,没有名为build
的默认方法用于Screen
小部件)。
Thus you may change MainWindow
to something like this,因此,您可以将MainWindow
更改为这样的东西,
class MainWindow(Screen):
def __init__(self,**kwargs):
super().__init__(**kwargs)
button_take_picture = Button(text = "take a picture")
button_take_picture.size_hint = (.5,.2)
button_take_picture.pos_hint = {"x": .25, "y": .25}
layout = BoxLayout()
lab = Label(text="some text")
layout.add_widget(button_take_picture)
layout.add_widget(lab)
self.add_widget(layout) # Add all those in this screen.
Similar rule applies for other screen(s).类似的规则适用于其他屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.