代码之家  ›  专栏  ›  技术社区  ›  raggot

Kivy函数调用中出现意外的TypeError

  •  1
  • raggot  · 技术社区  · 6 年前

    我将Kivy与python3.5结合使用。我正在试着设置一个应用程序,在这个应用程序中按下按钮可以安排函数调用。前提:这是我第一次使用Kivy,我对Python不是很在行。下面是我的简化代码:

    class MyApp(App):
        def build(self):
            layout = GridLayout(cols=2)
            TextStream = Label(text = 'Something will be written here: ')
            StartButton = Button(text = 'Start writing')
            StartButton.bind(on_press=lambda x:self.start_program(freq=10))
            layout.add_widget(TextStream)
            layout.add_widget(StartButton)
            return layout
    
        def start_program(self, freq):
            Clock.schedule_interval(self.write_something, 1.0/freq)
            # Also tried Clock.schedule_interval(self.write_something(), 1.0/freq)
    
        def write_something(self):
            TextStream.text =+ 'Something '
    
    if __name__ == '__main__':
        MyApp().run()
    

    运行脚本时得到的结果是:

    TypeError: write_something() takes 1 positional argument but 2 were given
    

    阅读答案 this question ,关于这个特定的错误,我理解Python中的方法实际上是以不同的方式实现的,而不是它们在代码中的显示方式。

    更准确地说,从 this comment 我理解正确声明方法 write_something 给它 self this answer

    那么,有没有一些沉默的争论通过了基维?我是不是用错误的方法调用了这个方法?

    编辑-错误堆栈

    Traceback (most recent call last):
    
      File "<ipython-input-1-5d17b5569154>", line 1, in <module>
        runfile('/home/raggot/Projects/MyApp/scripts/_test_kivy.py', wdir='/home/raggot/Projects/MyApp/scripts')
    
      File "/usr/local/lib/python3.5/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
        execfile(filename, namespace)
    
      File "/usr/local/lib/python3.5/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)
    
      File "/home/raggot/Projects/MyApp/scripts/_test_kivy_camera.py", line 78, in <module>
        MyApp().run()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/app.py", line 826, in run
        runTouchApp()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/base.py", line 502, in runTouchApp
        EventLoop.window.mainloop()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/core/window/window_pygame.py", line 403, in mainloop
        self._mainloop()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/core/window/window_pygame.py", line 289, in _mainloop
        EventLoop.idle()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/base.py", line 337, in idle
        Clock.tick()
    
      File "/usr/local/lib/python3.5/dist-packages/kivy/clock.py", line 581, in tick
        self._process_events()
    
      File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
    
      File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
    
      File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
    
      File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
    
    TypeError: write_something() takes 1 positional argument but 2 were given
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   raggot    6 年前

    schedule_interval Kivy传递额外的参数给它,因此它抛出错误,在你的情况下它只使用 args 你没有任何问题。如果是 Clock ,它通过了 dt

    def start_program(self, freq):
        Clock.schedule_interval(self.write_something, 1.0/freq)
    
    def write_something(self, *args):
        print(args)