사용자 도구

사이트 도구


language:python:kivy

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
language:python:kivy [2016/12/08 17:29] – [설치] kieunslanguage:python:kivy [2024/04/23 22:44] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== Installation ======
 +
 +필수
 +  * Cython
 +
 +옵션
 +  * OpenCV 2.0 : 카메라 입력
 +  * Pillow : 이미지와 텍스트 출력
 +  * PyEnchant : 스펠링
 +
 +다운로드
 +  * http://kivy.org/#download
 +
 +===== 설치 =====
 +
 +==== 윈도우즈 ====
 +
 +  * [[https://kivy.org/docs/installation/installation-windows.html|가이드]]
 +  * **Warning** 항목이 있으면 읽어봐야 함
 +
 +pip와 setuptools를 최신 버젼으로 업데이트
 +
 +<code>
 +python -m pip install --upgrade pip wheel setuptools
 +</code>
 +
 +연계항목 설치
 +
 +<code>
 +python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
 +python -m pip install kivy.deps.gstreamer --extra-index-url https://kivy.org/downloads/packages/simple/
 +</code>
 +
 +kivy 설치
 +
 +<code>
 +python -m pip install kivy
 +</code>
 +
 +====== 이벤트, 프로퍼티 Events and Properties ======
 +
 +===== 인트로 =====
 +
 +**EventDispatcher** : 이벤트를 등록하고 디스패치하는 중요 클래스
 +
 +이벤트를 대응 되는 클래스는,
 +  * Widget
 +  * Animation
 +  * Clock
 +
 +이벤트의 발생(실행은) MainLoop 가 관할한다.
 +
 +===== MainLoop =====
 +
 +<code python>
 +# import
 +from kivy.clock import Clock
 +</code>
 +
 +Kivy 의 메인루프가 있다.
 +이벤트에 대응하는 콜백도 이 메인루프에서 호출된다.
 +지연 시간이 길거나 무한 루프를 넣어버리면 앱이 바보가 된다.
 +
 +<code python>
 +while True:
 + animate_something()
 + time.sleep(.10)
 +</code>
 +
 +===== 주기적인 이벤트 함수 호출 =====
 +
 +schedule_interval() 을 사용해서 초당 X번 호출되도록 함수 또는 메쏘드를 추가할 수 있다.
 +
 +추가
 +
 +<code python>
 +def my_callback(dt):
 + print('My callback is called: ', dt)
 +event = Clock.schedule_interval(my_callback, 1/30)
 +</code>
 +
 +캔슬
 +
 +<code python>
 +# 방법-1
 +event.cancel()
 +# 방법-2
 +Clock.unschedule(event)
 +</code>
 +
 +콜백함수에서 False를 리턴하면 자동 종료 된다.
 +
 +===== 한번만 호출 =====
 +
 +**schedule_once()**는 콜백함수를 한번만 호출한다, 몇프레임 뒤 또는 몇초 후에.
 +
 +<code python>
 +def my_callback_once(dt):
 + print('it called !')
 +Clock.schedule_once(my_callback_once, 1)
 +# 1초 후에 호출된다. (in one second) 
 +</code>
 +
 +  * 파라미터 1번 : 호출할 콜백함수
 +  * 파라미터 2번 : 콜백함수를 호출할때까지의 대기시간. 단위는 초.(second)
 +
 +대기시간 파라미터의 의미는
 +
 +  * 0보다 크며 : X 초 후에 호출
 +  * 0 이면 : 다음 프레임 후에 호출한다.
 +  * -1 이면 : 다음 프레임 전에 호출된다.
 +
 +===== 트리거 =====
 +
 +용도가 이해가 잘 안되는데? 아무튼.
 +
 +<code python>
 +# 일, 한번만 호출되도록 설정
 +event = Clock.schedule_once(my_callback, 0)
 +# 그런데 다른 곳에서 이 콜백을 재설정 해야할 경우,
 +# 기존 것을 삭제하고 다시 스케쥴을 추가한다.
 +Clock.unschedule(event) 
 +event = Clock.schedule_once(my_callback, 0)
 +#
 +# 이렇게 쓰면 비용이 높답니다.
 +#
 +</code>
 +
 +schedule_once() 대신 필요한 시점에 호출 할 수 있는 이벤트로서 트리거를 사용하자.
 +
 +<code python>
 +trigger = Clock.create_trigger(my_callback)
 +# call later
 +trigger()
 +</code>
 +
 +**trigger()**를 호출할 때마다 한번한 실행되도록 설정된다. 이미 실행하도록 추가되어 있다면 추가되지 않는다.
 +
 +===== 위젯 이벤트 Widget events =====
 +
 +이벤트 종류 :
 +
 +  * 프로퍼티 이벤트 : 위젯에 변경이 있을 경우, 위치나 크기의 변경, 이벤트 발생
 +  * 위젯 디파인 이벤트 : 위젯에 정의된 이벤트, 버튼 눌림, 발생시
 +
 +===== 임의 이벤트 만들기 =====
 +
 +임의의 이벤트를 만들려면 (1) 이벤트 이름을 등록한다 (2) 이벤트 이름과 같은 메소드를 만든다. (3) 사용한다
 +
 +<code python>
 +from kivy.event import EventDispatcher
 +
 +class MyEventDispatcher(EventDispatcher):
 + def __init__(self,**kwargs):
 + self.register_event_type('on_test')
 + super(MyEventDispatcher, self).__init__(**kwargs)
 +
 + def do_something(self,value):
 + self.dispatch('on_test', value)
 +
 + def on_test(self, *args):
 + print('i am dispatched', args)
 +
 +</code>
 +
 +===== 콜백 붙이기 =====
 +
 +**바인딩**<sup>Binding</sup>
 +
 +<code python>
 +def my_callback(value, *args):
 + print('Hello, I got an event', args)
 +
 +ev = MyEventDispatcher()
 +ev.bind(on_test=my_callback)
 +ev.do_something('test')
 +</code>
 +
 +  * do_something() 가 실행 되면,
 +  * on_test에 바인딩 된, **my_callback**가 먼저 실행되고
 +  * **MyEventDispatcher.on_test()** 가 실행된다.
 +
 +===== 프로퍼티 =====
 +
 +Properties are an awesome way to define events and bind to them. 
 +
 +Essentially, they produce events such that when an attribute of your object changes, 
 +all properties that reference that attribute are automatically updated.
 +
 +There are different kinds of properties to describe the type of data you want to handle. 
 +
 +이벤트에 필요한 데이터 타입을 프로퍼티라고 하는거 같다.
 +
 +StringProperty / NumericProperty / BoundedNumericProperty / ObjectProperty / DictProperty
 +
 +ListProperty / OptionProperty / AliasProperty / BooleanProperty / ReferenceListProperty
 +
 +===== 프로퍼티 선언 =====
 +
 +<code python>
 +#
 +#
 +class MyWidget(Widget):
 + text = StringProperty('')
 +#
 +#
 +def __init__(self, **kwargs):
 + super(MyWidget, self).__init__(**kwargs)
 +</code>
 +
 +===== 디스패칭 프로퍼티 이벤트 =====
 +
 +Kivy의 프로퍼티는 'on_<property_name>' 같은 이름의 이벤트로  제공된다.
 +
 +이 이벤트는 프로퍼티의 값이 바뀔때 이벤트가 호출된다.
 +프로퍼티의 값이 전,후가 같은 경우 이벤트는 발생되지 않는다.
 +
 +hmm..
 +
 +<code python>
 +class CustomBtn(Widget):
 + pressed = ListProperty([0,0])
 + def on_touch_down(self, touch):
 + if self.collide_point(*touch.pos):
 + self.pressed = touch.pos
 + return True
 + return super(CustomBtn, self).on_touch_down(touch)
 + def on_pressed(self, instance, pos):
 + print('pressed at {pos}'.format(pos=pos))
 +</code>
 +
 +===== 커스텀 Custom 프로퍼티 =====
 +
 +?? 재정리
 +
 +====== 위젯 Widget ======
 +그려러니 하는 부분은 넘어가자.
 +
 +===== 위젯 트리 조작 Manipulating the Widget tree =====
 +
 +root widget 에서 시작되고 자식 위젯을 가질 수 있고, 자식 위젯은 또 자식 위젯을 가질 수 있다.
 +이 형식은 tree 형태로 구성된다.
 +자식 위젯들(children)는 ListProperty 속성으로 저장된다.
 +
 +  * add_widget() : 위젯 추가
 +  * remove_widget() : 위젯 삭제
 +  * clear_widgets() : 위젯에 포함된 모든 자식 위젯 삭제
 +
 +
 +===== 위젯 트리 탐색 =====
 +
 +===== 위젯의 Z 인덱스 =====
 +
 +===== 레이아웃으로 화면 구성 =====
 +
 +===== 레이아웃에 배경 추가 =====
 +
 +===== Nesting Layouts =====
 +
 +===== 크기와 위치의 단위 =====
 +
 +===== Screen Separation with Screen Manager =====
 +
 +
 +====== Graphics ======
 +
 +===== 캔바스 =====
 +
 +===== Context instructions =====
 +
 +===== 드로잉 instructions =====
 +
 +===== 조작  instructions =====
 +
 +
 +====== Kv 언어 ======
 +
 +====== 다른 프레임워크와 함께 ======
 +