Reputation: 837
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.factory import Factory
class My3App(App):
def build(self):
return Hello()
class Hello(Widget):
def btn(self):
pass
if __name__ == "__main__":
My3App().run()
kv File
<Hello>:
Button:
text : "button1"
on_press : root.btn()
How do I create a Popup when I click on button1? I want a large popup which contains so many buttons and labels. How do I write that popup in kv language?
Thanks in advance
Upvotes: 1
Views: 3493
Reputation: 16031
How do I create a Popup when I click on button1?
In this solution, it illustrates the use of dynamic class, and it involves changing both kv file and Python script.
Factory
CustomPopup
with inheritance of Popup
widgeton_release
event for button1 which use Kivy Factory
to register and instantiate dynamic class, CustomPopup
. Use Popup's open()
method to display the Popup's window.#:import Factory kivy.factory.Factory
...
<CustomPopup@Popup>:
...
<Hello>:
Button:
text : "button1"
on_release: Factory.CustomPopup().open()
btn()
method from class Hello()
pass
to class Hello()
class Hello(Widget):
pass
How do I write that popup in kv language?
There are two methods to implementing a class with Popup inheritance.
In this method, it involves changes to only kv file. We added a dynamic class rule.
Snippets - kv#:import Factory kivy.factory.Factory
<CustomPopup@Popup>:
title: 'Test popup'
size_hint: (None, None)
size: (400, 400)
Label:
text: 'Hello world'
<Hello>:
Button:
text : "button1"
on_release: Factory.CustomPopup().open()
In this method, it involves changes to both kv file and Python script. We added a class rule in kv file, and implement a class in Python script.
Snippets - kv<CustomPopup>:
title: 'Test popup'
size_hint: (None, None)
size: (400, 400)
Label:
text: 'Hello world'
<Hello>:
Button:
text : "button1"
on_press: root.btn()
Snippets - Py
from kivy.uix.popup import Popup
...
class CustomPopup(Popup):
pass
class Hello(Widget):
def btn(self):
popup = CustomPopup()
popup.open()
I want a large popup which contains so many buttons and labels.
In this solution, we will be using dynamic class, and add buttons and labels into kv file. When creating a popup, you must at least set a Popup.title and Popup.content. The Popup.content
can be any widget e.g. Screen, Layouts (BoxLayout, GridLayout, etc), Label, Button, etc. With a BoxLayout or GridLayout, you can add Label, and Button.
#:import Factory kivy.factory.Factory
<CustomPopup@Popup>:
title: 'My Custom Kivy Popup'
auto_dismiss: False
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint: 1, 0.9
Label:
text: 'Hello'
Label:
text: 'Kivy'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Linux'
Label:
text: 'Python'
GridLayout:
rows: 1
row_force_default: True
row_default_height: '46dp'
col_force_default: True
col_default_width: '200dp'
Button:
text: 'Close this popup'
on_release: root.dismiss()
Button:
text: 'Cancel'
on_release: root.dismiss()
<Hello>:
Button:
text : "button1"
on_release: Factory.CustomPopup().open()
Upvotes: 4
Reputation: 355
you should check this tutorial: https://kivy.org/doc/stable/api-kivy.uix.popup.html
MyPopup = Popup(title='Test popup', content=Label(text='Hello world'),
auto_dismiss=False
Button:
text: 'Open popup'
on_release: Factory.MyPopup().open()
Upvotes: -2