在 Python + Kivy 中给按钮添加事件时,windows 自动关闭

When adding an event to a button in Python + Kivy, the windows closes automatically

我正在学习 Python + Kivy,window 在向按钮添加事件时会自动关闭。但是,当我添加一个不执行任何操作的按钮时,该应用程序完全正常。

我尝试添加一个没有事件的按钮,并浏览了各种教程和论坛。似乎没有任何帮助。另外,我 copy/pasted 我的代码中带有按钮的行之后认为有错字。没用。

进口:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

按钮和事件:

self.submit = Button(text="Submit", font_size=20)
self.submit.bind(on_press=self.submit)
self.add_widget(self.submit)

定义函数:

def submit(self, instance):
        print('You pressed the button!')

终端输出:

bob@I-am-Bob:~/Programs/Kivy_Python$ python3 main.py 
[INFO   ] [Logger      ] Record log in /home/bob/.kivy/logs/kivy_19-07-02_17.txt
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "/home/bob/.local/lib/python3.6/site-packages/kivy/__init__.py"
[INFO   ] [Python      ] v3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]
[INFO   ] [Python      ] Interpreter at "/usr/bin/python3"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] Backend used <sdl2>
[INFO   ] [GL          ] OpenGL version <b'3.0 Mesa 18.0.5'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO   ] [GL          ] OpenGL renderer <b'Mesa DRI Intel(R) Haswell Mobile '>
[INFO   ] [GL          ] OpenGL parsed version: 3, 0
[INFO   ] [GL          ] Shading version <b'1.30'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Clipboard   ] Provider: gtk3(['clipboard_xclip', 'clipboard_xsel', 'clipboard_dbusklipper'] ignored)
[CRITICAL] [Cutbuffer   ] Unable to find any valuable Cutbuffer provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
xclip - FileNotFoundError: [Errno 2] No such file or directory: 'xclip': 'xclip'
  File "/home/bob/.local/lib/python3.6/site-packages/kivy/core/__init__.py", line 63, in core_select_lib
    fromlist=[modulename], level=0)
  File "/home/bob/.local/lib/python3.6/site-packages/kivy/core/clipboard/clipboard_xclip.py", line 17, in <module>
    p = subprocess.Popen(['xclip', '-version'], stdout=subprocess.PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)

xsel - FileNotFoundError: [Errno 2] No such file or directory: 'xsel': 'xsel'
  File "/home/bob/.local/lib/python3.6/site-packages/kivy/core/__init__.py", line 63, in core_select_lib
    fromlist=[modulename], level=0)
  File "/home/bob/.local/lib/python3.6/site-packages/kivy/core/clipboard/clipboard_xsel.py", line 16, in <module>
    p = subprocess.Popen(['xsel'], stdout=subprocess.PIPE)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)

 Traceback (most recent call last):
   File "main.py", line 43, in <module>
     Hello().run()
   File "/home/bob/.local/lib/python3.6/site-packages/kivy/app.py", line 829, in run
     root = self.build()
   File "main.py", line 40, in build
     return HiGrid()
   File "main.py", line 32, in __init__
     self.submit.bind(on_press=self.submit)
   File "kivy/_event.pyx", line 419, in kivy._event.EventDispatcher.bind
 AssertionError: <kivy.uix.button.Button object at 0x7ff3e36a15f8> is not callable

我希望 window 保持打开状态并且终端打印 'You pressed the button!'。但是,window 会自动关闭。

它不起作用,因为 self.submitButton 小部件的一个实例,而您正在绑定到它自己。

解决方案

用另一个名字替换self.submit,例如button

片段

button = Button(text="Submit", font_size=20)
button.bind(on_press=self.submit)
self.add_widget(button)