简体   繁体   中英

How to open a popup after clicking on button in python kivy?

I am trying to open popup after clicking on button using python kivy but getting an error

Here is my code:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.config import Config


Config.set("graphics", "resizable", 0)
Config.set("graphics", "width", 400)
Config.set("graphics", "height", 500)


class myLayout(BoxLayout):
    def __init__(self, **kwargs):
        super(myLayout, self).__init__(**kwargs)

        btn = Button(text="Click")
        btn.bind(on_press=self.clk)

        self.add_widget(btn)

    def clk(self, obj):

        popup = Popup(content="I am popup")
        return popup.open()


class ReminderApp(App):
    def build(self):
        mL = myLayout()
        return mL


if __name__ == "__main__":
    ReminderApp().run()

It works if in clk function instead of popup just print something, for eg:

    def clk(self, obj):
    print("Hello world")

Help me please, I'm stuck

Here is the error message:

Traceback (most recent call last):
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 619, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy/weakproxy.pyx", line 33, in kivy.weakproxy.WeakProxy.__setattr__
   File "kivy/properties.pyx", line 478, in kivy.properties.Property.__set__
   File "kivy/properties.pyx", line 516, in kivy.properties.Property.set
   File "kivy/properties.pyx", line 571, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/popup.py", line 223, in on__container
     self._container.add_widget(self.content)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/boxlayout.py", line 312, in add_widget
     widget.fbind('pos_hint', self._trigger_layout)
 AttributeError: 'str' object has no attribute 'fbind'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "main.py", line 40, in <module>
     ReminderApp().run()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/app.py", line 826, in run
     runTouchApp()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", line 723, in mainloop
     self._mainloop()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/core/window/window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/base.py", line 340, in idle
     self.dispatch_input()
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/base.py", line 231, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/core/window/__init__.py", line 1352, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/core/window/__init__.py", line 1368, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/behaviors/button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
   File "main.py", line 25, in clk
     popup = Popup(content="I am popup")
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/modalview.py", line 152, in __init__
     super(ModalView, self).__init__(**kwargs)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/anchorlayout.py", line 68, in __init__
     super(AnchorLayout, self).__init__(**kwargs)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/layout.py", line 76, in __init__
     super(Layout, self).__init__(**kwargs)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/widget.py", line 348, in __init__
     Builder.apply(self, ignored_consts=self._kwargs_applied_init)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 469, in apply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 630, in _apply_rule
     e), cause=tb)
 kivy.lang.builder.BuilderException: Parser: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/data/style.kv", line 506:
 ...
     504:# Popup widget
     505:<Popup>:
 >>  506:    _container: container
     507:    GridLayout:
     508:        padding: '12dp'
 ...
 AttributeError: 'str' object has no attribute 'fbind'
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 619, in _apply_rule
     setattr(widget_set, key, value)
   File "kivy/weakproxy.pyx", line 33, in kivy.weakproxy.WeakProxy.__setattr__
   File "kivy/properties.pyx", line 478, in kivy.properties.Property.__set__
   File "kivy/properties.pyx", line 516, in kivy.properties.Property.set
   File "kivy/properties.pyx", line 571, in kivy.properties.Property.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1120, in kivy._event.EventObservers._dispatch
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/popup.py", line 223, in on__container
     self._container.add_widget(self.content)
   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/boxlayout.py", line 312, in add_widget
     widget.fbind('pos_hint', self._trigger_layout)

Exception ignored in: functools.partial(<function _widget_destructor at 0x102eaad08>, 23)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/widget.py", line 265, in _widget_destructor
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 760, in unbind_widget
  File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'
Exception ignored in: functools.partial(<function _widget_destructor at 0x102eaad08>, 34)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/widget.py", line 265, in _widget_destructor
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 760, in unbind_widget
  File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'
Exception ignored in: functools.partial(<function _widget_destructor at 0x102eaad08>, 39)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/uix/widget.py", line 265, in _widget_destructor
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/kivy/lang/builder.py", line 760, in unbind_widget
  File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'weakref' object has no attribute 'cline_in_traceback'

Problem

The error that you encountered was due to content which is an ObjectProperty and assigned a string, "I am popup" but it was expecting a widget.

Popup

content
Content of the popup that is displayed just under the title.
content is an ObjectProperty and defaults to None.

Solution

Add a Label widget to content as shown below.

Snippets

def clk(self, obj):

    popup = Popup(content=Label(text="I am popup"))
    popup.open()

Example

main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.config import Config


Config.set("graphics", "resizable", 0)
Config.set("graphics", "width", 400)
Config.set("graphics", "height", 500)


class myLayout(BoxLayout):
    def __init__(self, **kwargs):
        super(myLayout, self).__init__(**kwargs)

        btn = Button(text="Click")
        btn.bind(on_press=self.clk)

        self.add_widget(btn)

    def clk(self, obj):

        popup = Popup(content=Label(text="I am popup"))
        popup.open()


class ReminderApp(App):
    def build(self):
        mL = myLayout()
        return mL


if __name__ == "__main__":
    ReminderApp().run()

Output

Img01-应用启动 Img02-弹出窗口

The Popup may well be opening, but is getting automatically closing again, if the button click is slightly held onto.

Try:

popup = Popup(content=content, auto_dismiss=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM