Kivy: Multiple items in a popup wont work

Question:

I am trying to use multiple items in a popup with kivy in python. I would
like to find out how to make this work. I am not sure if it has to do with the fact
that the script is on my phone and not made for computer.
Here is a little example script that I am asking you to help make work for future reference.

import kivy
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label

popup = Popup(title='Test popup',
    content=Label(text='Hello world'),
            TextInput(text='Hi'),        #Here is what I am trying to make work
    size_hint=(None, None), size=(400, 400))

So you can see it is two objects in one content of the popup. I am sure this is possible
because I’ve seen it on kivy apps in the appstore, but not sure how to do it myself.

Asked By: Kogan007

||

Answers:

The content of a Popup can only be a single widget. You cannot add two widgets like you’re trying to do.

To accomplish what you’re trying to do you’ll have to add the label and text input to e.g. a boxlayout and then add the boxlayout to content. Here’s an example that should work:

from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

box = BoxLayout()
box.add_widget(Label(text='Hello world'))
box.add_widget(TextInput(text='Hi'))

popup = Popup(title='Test popup', content=box, size_hint=(None, None), size=(400, 400))
Answered By: Matt

You can achieve this using .kv file.

<Content>:
    orientation:'vertical'
    Label:
       text: 'Hello World'
    Button:
        text: 'Press Me'

In python file:

    def openPop(self):
        self.pop = Popup(title='Test',content=Content(),auto_dismiss=True)
        self.pop.open()
Answered By: SagitSri

you need to do this:

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.super_box = BoxLayout(orientation = "vertical")
        self.pn = TextInput(text = "projectname") # TODO: Project name input
        self.super_box.add_widget(self.pn)
        """Create button: Creates a new project repo"""
        cb = Button(text="Create")
        cb.bind(on_release = lambda x: print("a new project repo gets created"))
        self.super_box.add_widget(cb)
Answered By: user17252523
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.