>

Kivy를 사용하여 간단한 인터페이스, 맨 위에 제목 표시 줄, 맨 아래에 텍스트 입력 및 그 옆에 작은 버튼 "add"가있는 간단한 인터페이스를 사용하여 Python으로 코딩하려고합니다. 상단과 하단 사이에서 스크롤보기를 원하고 텍스트 입력 옆에있는 버튼을 클릭하면 스크롤보기에서 새 버튼이 생성되고 그 옆에 작은 버튼이 생성되어 줄을 삭제합니다 (line = the 버튼 + 삭제 버튼) 이해하다:

삭제하기 위해 "X"버튼을 클릭하면 오류가 발생합니다 :

Traceback (most recent call last):
   File "nouv_test.py", line 39, in <module>
     RunApp().run()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
     EventLoop.idle()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\base.py", line 342, in idle
     self.dispatch_input()
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\base.py", line 327, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\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:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\window\__init__.py", line 1192, in on_motion
     self.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\core\window\__init__.py", line 1228, in on_touch_up
     if w.dispatch('on_touch_up', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\screenmanager.py", line 1199, in on_touch_up
     return super(ScreenManager, self).on_touch_up(touch)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\widget.py", line 479, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\relativelayout.py", line 304, in on_touch_up
     ret = super(RelativeLayout, self).on_touch_up(touch)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\widget.py", line 479, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\widget.py", line 479, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\scrollview.py", line 833, in on_touch_up
     if self.dispatch('on_scroll_stop', touch):
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\scrollview.py", line 872, in on_scroll_stop
     self.simulate_touch_down(touch)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\scrollview.py", line 596, in simulate_touch_down
     ret = super(ScrollView, self).on_touch_down(touch)
   File "C:\Users\dvfrg\AppData\Local\Programs\Python\Python36\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:\Users\dvfrg\AppData\Local\Programs\Python\Python36\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:\Users\dvfrg\AppData\Local\Programs\Python\Python36\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy\_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy\_event.c:8191)
 TypeError: 'NoneType' object is not callable

내 코드 (.py/.kv)는 다음과 같습니다.

# coding: utf-8
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder

from kivy.uix.button import Button

class GUI(ScreenManager):
    def __init__(self, **kwargs):
        super(GUI, self).__init__(**kwargs)
class Home(Screen):
    def __init__(self, **kwargs):
        super(Home, self).__init__(**kwargs)
    def add_btn(self, grid):
        left = Button(text="added", size = (32, 32), size_hint = (1, None)) # the green button
        right = Button(background_color = (1,0,0,1), text="X", size = (32, 32), size_hint = (None, None)) # the red DELETE button
        right.on_press = self.del_btn(grid, left, right)
        grid.add_widget(left)
        grid.add_widget(right)
    def del_btn(self, grid, L, R): # function called when I click on delete a line
        grid.remove_widget(L)
        grid.remove_widget(R)
Builder.load_file("nouv_test.kv")
class RunApp(App):
    def build(self):
        return GUI()
if __name__ == '__main__':
    RunApp().run()

.KV :

#: kivy 1.10.0
    <GUI>:
        Home:
            name: "home_screen"
            id: home
    <Home>:
        GridLayout:
            rows: 3
            Button:
                text: u"Mes ann\u00e9es"
                size: (50, 50)
                size_hint: (1, None)
                background_color: (0,1,0,1)
            ScrollView:

                size_hint:(1, .8)
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                do_scroll_x: False
                GridLayout:
                    id: home_scroll_grid
                    cols: 2
                    padding: 5
                    spacing: 5
                    height: self.minimum_height
                    size_hint: (1, None)

            BoxLayout:
                padding: 5
                spacing: 5
                size: (42, 42)
                size_hint: (1, None)
                TextInput:
                    size: (32, 32)
                    size_hint: (1, None)
                Button:
                    text: "+"
                    background_color: (0,1,0,1)
                    size: (32, 32)
                    size_hint: (None, None)
                    on_press: root.add_btn(home_scroll_grid)


  • 답변 # 1

    당신은 NoneType 를 얻는다  이 명령문에서 오류 : right.on_press = self.del_btn(grid, left, right) 당신은 실제로 del_btn 를 호출 이며 아무것도 반환하지 않습니다.
    와이즈 비즈  호출 된 함수가 아닌 호출 가능을 사용합니다. 따라서 다음과 같이 보일 것입니다 : on_press .

    그러나 콜백에 인수를 전달해야하므로 right.on_press = self.del_btn 를 사용할 수 있습니다
    그런 다음 partial 를 사용하십시오.  on_press에 할당하는 대신.
    이와 같이 :

    .bind
    
    

    그런 다음 :

    from functools import partial
    
    
    right.bind(on_press=partial(self.del_btn, grid, left, right))

관련 자료

  • 이전 rust - 특성 오브젝트의&mut과 ref mut의 차이점
  • 다음 python - Tkinter 이름"X"는 정의되지 않은 오류입니다