Nabajyoti Das
Nabajyoti Das

Reputation: 524

how to load an image as a popup in kivy

def pop1(self):
    pop = Popup(title='test',content=Image('boy.png'),
                size_hint=(None,None))
    pop.open()

Then in the kv language i tried to call it from button on_press event. the code for the kv file is this.

    BoxLayout:
    size:self.parent.size
    orientation:'horizontal'
    spacing:10
    padding:5
    Button:
        text:
        size_hint:.3,.3
        on_press: 
            root.pop1()

when i try to run it, i get an error as follows:

: _container: container GridLayout: padding: 12 keyError: 'pos_hint'

Upvotes: 3

Views: 2789

Answers (1)

Matt
Matt

Reputation: 1347

First of all, since you call root.pop1() and root in this case is a BoxLayout I assume that you defined your pop1 function in a class you called BoxLayout inheriting from BoxLayout? If so, you're basically overwriting a kivy class, which will mess things up.

A second issue is that you call Image('boy.png') when it should be Image(source='boy.png').

Finally, in the button, you forgot to set text to a value.

A better way of doing what you're trying to accomplish is with the following code:

from kivy.app import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from kivy.lang import Builder


kv = '''
BoxLayoutWithPopup:
    orientation:'horizontal'
    spacing:10
    padding:5
    Button:
        text: 'Press me'
        size_hint:.3,.3
        on_press:
            root.pop1()
'''


class BoxLayoutWithPopup(BoxLayout):

    def pop1(self):
        pop = Popup(title='test', content=Image(source='boy.png'),
                    size_hint=(None, None), size=(400, 400))
        pop.open()


if __name__ == '__main__':
    runTouchApp(Builder.load_string(kv))

Upvotes: 6

Related Questions