G
G
golub4ik2018-06-03 06:50:17
Python
golub4ik, 2018-06-03 06:50:17

Python + Kivy -TypeError: * takes 1 positional argument but 2 were given how to deal with it?

I get this error: TypeError: generator() takes 1 positional argument but 2 were given
I want to write hello world to the console when the button is pressed, but something goes wrong. Thanks in advance!

from kivy.app import App

from kivy.uix.button import	Button
from kivy.uix.gridlayout import	GridLayout

from	kivy.config import	Config
Config.set('graphics', 'resizable', 0)
Config.set('graphics', 'height', 500)
Config.set('graphics', 'width', 500)


class PasswordGeneratorApp(App):

  def generator(self):
    print('Hello world!')

  def build(self):

    appbuild = GridLayout()

    appbuild.add_widget( Button(text = "сгенерировать", background_normal = "", background_color = [.96, .77, .15, 1], on_press = self.generator))
    
    return appbuild

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

Mistake:
Traceback (most recent call last):
   File "pass.py", line 26, in <module>
     PasswordGeneratorApp().run()
   File "C:\Python365\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Python365\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Python365\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "C:\Python365\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
     EventLoop.idle()
   File "C:\Python365\lib\site-packages\kivy\base.py", line 342, in idle
     self.dispatch_input()
   File "C:\Python365\lib\site-packages\kivy\base.py", line 327, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Python365\lib\site-packages\kivy\base.py", line 233, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Python365\lib\site-packages\kivy\core\window\__init__.py", line 1188, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Python365\lib\site-packages\kivy\core\window\__init__.py", line 1204, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Python365\lib\site-packages\kivy\uix\widget.py", line 457, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Python365\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy\_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8146)
   File "kivy\_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy\_event.c:14035)
   File "kivy\_event.pyx", line 1149, in kivy._event.EventObservers._dispatch (kivy\_event.c:13564)
 TypeError: generator() takes 1 positional argument but 2 were given

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Сергей Горностаев, 2018-06-03
@golub4ik

Метод generator у вас объявлен как принимающий только self, но потом передаётся в кнопку как обработчик нажатия, а обработчикам нажатия передаётся параметр события. Исправьте объявление функции на def generator(self, event):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question