I'm trying to create a simple Screen with Kivy.
Here's my code:
class invoice_screen(Screen):
def __init__(self, **kwargs):
super(invoice_screen, self).__init__(**kwargs)
self._window = BoxLayout(orientation='vertical')
self._window.add_widget(Label(text="something or another"))
scroll = ScrollView()
box = BoxLayout(orientation='vertical')
top_row = BoxLayout(orientation='horizontal')
top_row.add_widget(
Label(
text="item",
size_hint=(.8, 1)
)
)
top_row.add_widget(
Label(
text="quantity",
size_hint=(.2, 1)
)
)
box.add_widget(bot_row)
scroll.add_widget(box)
self._window.add_widget(scroll)
buttons = BoxLayout(orientation='horizontal')
button1 = Button(text="Complete!")
button1.bind(on_release=self.forward)
button2 = Button(text="Go Back")
button2.bind(on_release=self.back)
buttons.add_widget(button1, button2)
self._window.add_widget(buttons)
def back(self):
pass
def forward(self):
Popup(
title="Completed",
content=Label(text="You can close this NOW!"),
size=(400,400)
)
class app(App):
def build(self):
mane_chan = ScreenManager()
mane_chan.add_widget(invoice_screen(name="invoice"))
mane_chan.current = "invoice"
return mane_chan
if __name__ == "__main__":
app().run()
But when I run it, only the Kivy window shows up. There aren't any of the elements I've included in the Screen. There weren't even any errors.
I've got no idea what I'm doing wrong here.
You add all widgets to self._window
( BoxLayout
) but you forgot to add this self._window
to screen
( self
)
self.add_widget(self._window)
That's all.
Full working code with other small fixes - see comments in code
# https://kivy.org/doc/stable/guide/basic.html
#import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
class InvoiceScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._window = BoxLayout(orientation='vertical')
self.add_widget(self._window) # <--- need to add `boxlayout` to `screen`
self._window.add_widget(Label(text="something or another"))
scroll = ScrollView()
box = BoxLayout(orientation='vertical')
top_row = BoxLayout(orientation='horizontal')
top_row.add_widget(
Label(
text="item",
size_hint=(.8, 1)
)
)
top_row.add_widget(
Label(
text="quantity",
size_hint=(.2, 1)
)
)
box.add_widget(top_row) # <-- it has to be `top_row` instead of `bot_row`
scroll.add_widget(box)
self._window.add_widget(scroll)
buttons = BoxLayout(orientation='horizontal')
button1 = Button(text="Complete!")
button1.bind(on_release=self.forward)
button2 = Button(text="Go Back")
button2.bind(on_release=self.back)
#buttons.add_widget(button1, button2) # <-- wrong
buttons.add_widget(button1) # <-- it has to add only one widget
buttons.add_widget(button2) # <-- it has to add only one widget
self._window.add_widget(buttons)
def back(self, event):
pass
def forward(self, event):
Popup(
title="Completed",
content=Label(text="You can close this NOW!"),
size=(400,400)
).open() # <-- forgot `.open()`
class app(App):
def build(self):
mane_chan = ScreenManager()
mane_chan.add_widget(InvoiceScreen(name="invoice"))
mane_chan.current = "invoice"
return mane_chan
if __name__ == "__main__":
app().run()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.