Rahul charan
Rahul charan

Reputation: 837

How to write popup in kv file when one press button

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

Answers (2)

ikolim
ikolim

Reputation: 16031

Question 1

How do I create a Popup when I click on button1?

Solution

In this solution, it illustrates the use of dynamic class, and it involves changing both kv file and Python script.

kv file

  • Add import statement of Kivy Factory
  • Add a dynamic class, CustomPopup with inheritance of Popup widget
  • Add on_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.
Snippets - kv
#:import Factory kivy.factory.Factory
...
<CustomPopup@Popup>:
...
<Hello>:
    Button:
        text : "button1"
        on_release: Factory.CustomPopup().open()

Py file

  • Remove btn() method from class Hello()
  • Add pass to class Hello()
Snippets - Py
class Hello(Widget):
    pass

Question 2

How do I write that popup in kv language?

Solution

There are two methods to implementing a class with Popup inheritance.

Method 1 - Using Dynamic Class

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()

Method 2 - Using Normal Class

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()

Question 3

I want a large popup which contains so many buttons and labels.

Solution

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.

Snippets - kv

#: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

Abd El Kodous Souissi
Abd El Kodous Souissi

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

Related Questions