单击python kivy中的按钮后如何打开弹出窗口?
Posted
技术标签:
【中文标题】单击python kivy中的按钮后如何打开弹出窗口?【英文标题】:How to open a popup after clicking on button in python kivy? 【发布时间】:2018-11-07 10:07:09 【问题描述】:我正在尝试使用 python kivy 单击按钮后打开弹出窗口,但出现错误
这是我的代码:
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()
如果在 clk
函数中而不是仅弹出 print
某些东西,它就可以工作,例如:
def clk(self, obj):
print("Hello world")
请帮帮我,我卡住了
这是错误信息:
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'
【问题讨论】:
【参考方案1】:问题
您遇到的错误是由于 content 这是一个 ObjectProperty 并分配了一个字符串 "I am popup" 但它是期待一个小部件。
Popup
content 显示在标题下方的弹出窗口的内容。 content 是一个 ObjectProperty,默认为 None。
解决方案
向content添加一个Label小部件,如下所示。
片段
def clk(self, obj):
popup = Popup(content=Label(text="I am popup"))
popup.open()
示例
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()
输出
【讨论】:
是否需要return
:return popup.open()
?我认为应该只是popup.open()
@nikkitikitavi:不客气。请记住将答案标记为已接受。谢谢。【参考方案2】:
弹出窗口很可能正在打开,但如果稍微按住按钮单击,它会再次自动关闭。
试试:
popup = Popup(content=content, auto_dismiss=False)
【讨论】:
以上是关于单击python kivy中的按钮后如何打开弹出窗口?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android studio 的弹出窗口内添加滚动的 listView?