Wie bekomme ich mein Popup-Fenster dynamisch an verschiedene Handy-Screens angepasst?
Ich versuche es so anzupassen, dass das Popup immer 80 % der Höhe und Breite des Handy-Screens einnimmt.
Oder nimmt man da metrics?
.py
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
Builder.load_file("main.kv")
class P(Popup):
label_text = StringProperty()
def __init__(self, title, content, **kwargs):
super(P, self).__init__(**kwargs)
self.title = title
self.label_text = content
self.size_hint = (None, None)
# self.size = (dp(400), dp(400))
self.size = (self.width - 20, self.height - 20)
print(self.size)
# self.center = (self.width / 2, self.height / 2)
class FirstScreen(Screen):
@staticmethod
def show_popup(screen):
content = f"(1) This is the content of the\ncustom popup from {screen.name}"
P(title=screen.name, content=content).open()
@staticmethod
def on_press():
print("on_press")
class TestApp(App):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(FirstScreen(name="first_screen"))
return screen_manager
if __name__ == "__main__":
TestApp().run()
Code: Alles auswählen
<P>:
BoxLayout:
orientation: 'vertical'
Label:
text: root.label_text
size_hint: dp(.6), dp(.2)
pos_hint: {"x": dp(.2), "top": dp(1)}
Button:
text: "Ok"
size_hint_y: None
height: dp(40)
pos_hint: {"x": dp(0), "y": dp(.1)}
on_press: root.dismiss()
<FirstScreen>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Show Popup'
on_press: root.show_popup(root)
Button:
text: 'Go to Second Screen'
on_press:
root.on_press()