2013-08-10 1 views
5

Gtk-3 용으로 작성된 가장 간단한 사용자 정의 위젯 예제를 찾으려고합니다.GTK3로 사용자 정의 위젯 작성

지금까지 발견 된 가장 좋은 점은 this (PyGTK 사용)이지만 Gtk-2를 타겟으로 한 것 같습니다.

BTW : 필자가 작성한 언어는 신경 쓰지 않지만 C++을 피할 수 있다면 훨씬 좋습니다!

답변

0

다음은 C로 http://ptomato.name/advanced-gtk-techniques/html/custom-container.html GTK 3 맞춤 컨테이너 위젯을 작성하는 방법입니다. 간단한 위젯을 작성하는 데 필요한 것보다 훨씬 복잡합니다. 또한 3 GTK 2에서 마이그레이션 가이드를 확인할 수 있습니다 https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html

+0

이것은 귀하의 블로그에서 가져온 것입니까? "이것은 GTK 2에서 일하는 방식과는 다소 다르며 아마도 인터넷 상에 떠 다니는 GTK 2 명령이 많이있을 것입니다." GTK 3 기술이라고 명시 적으로 말하면 더 명확하게 표현할 것을 권장합니다. – knocte

+0

확실하지 샘플을 제공하지 않기 때문에이 답변을 솔직하게 표시 할 것입니다. 그러나 링크를 이용해 주셔서 감사합니다 – knocte

+0

맞아요, 내 질문에 만족스럽게 대답하지 않는다고 생각합니다. :-( – ptomato

11

Python3 Gtk3는 그것이 다음 : 여기

from gi.repository import Gtk 

class SuperSimpleWidget(Gtk.Label): 
    __gtype_name__ = 'SuperSimpleWidget' 

실제로 무언가를 비 사소한 예입니다, 즉 페인트의 배경을 만들고 그것을 통해 대각선을 그립니다. 나는 (아래 참조) 일부 상용구를 저장 Gtk.Misc 대신 Gtk.Widget에서 상속 해요 :

class SimpleWidget(Gtk.Misc): 
    __gtype_name__ = 'SimpleWidget' 

    def __init__(self, *args, **kwds): 
     super().__init__(*args, **kwds) 
     self.set_size_request(40, 40) 

    def do_draw(self, cr): 
     # paint background 
     bg_color = self.get_style_context().get_background_color(Gtk.StateFlags.NORMAL) 
     cr.set_source_rgba(*list(bg_color)) 
     cr.paint() 
     # draw a diagonal line 
     allocation = self.get_allocation() 
     fg_color = self.get_style_context().get_color(Gtk.StateFlags.NORMAL) 
     cr.set_source_rgba(*list(fg_color)); 
     cr.set_line_width(2) 
     cr.move_to(0, 0) # top left of the widget 
     cr.line_to(allocation.width, allocation.height) 
     cr.stroke() 

당신이 정말로 Gtk.Widget에서 파생하려는 경우 마지막으로, 당신은 또한 그림의 배경을 설정해야합니다. Gtk.Misc이 그 일을하지만, Gtk.Widget은 실제로 그 자체로 무언가를 그려 내지 않는 컨테이너 일 수 있습니다. 그러나 문의 마음을 알고 싶어요, 그래서 당신은과 같이 그것을 할 수 :

from gi.repository import Gdk 

class ManualWidget(Gtk.Widget): 
    __gtype_name__ = 'ManualWidget' 

    def __init__(self, *args, **kwds): 
     # same as above 

    def do_draw(self, cr): 
     # same as above 

    def do_realize(self): 
     allocation = self.get_allocation() 
     attr = Gdk.WindowAttr() 
     attr.window_type = Gdk.WindowType.CHILD 
     attr.x = allocation.x 
     attr.y = allocation.y 
     attr.width = allocation.width 
     attr.height = allocation.height 
     attr.visual = self.get_visual() 
     attr.event_mask = self.get_events() | Gdk.EventMask.EXPOSURE_MASK 
     WAT = Gdk.WindowAttributesType 
     mask = WAT.X | WAT.Y | WAT.VISUAL 
     window = Gdk.Window(self.get_parent_window(), attr, mask); 
     self.set_window(window) 
     self.register_window(window) 
     self.set_realized(True) 
     window.set_background_pattern(None) 

편집 실제로이 기능을 사용하려면

w = Gtk.Window() 
w.add(SimpleWidget()) 
w.show_all() 
w.present() 
import signal # enable Ctrl-C since there is no menu to quit 
signal.signal(signal.SIGINT, signal.SIG_DFL) 
Gtk.main() 

또는 더 많은 재미의에서 직접 사용 ipython3 REPL :

from IPython.lib.inputhook import enable_gtk3 
enable_gtk3() 
w = Gtk.Window() 
w.add(SimpleWidget()) 
w.show_all() 
w.present() 
+0

cool! 그것을 렌더링하는 간단한 gtk 앱으로 어떻게 실행합니까? – knocte

0

지금까지 최고의 기준에 :
- 유 nderstand G 객체 (느릅 나무에서 GTK 위젯 파생)
은 - 일부 보일러 코드와 C 코드

https://developer.gnome.org/gobject/stable/howto-gobject.html

나는 그것을 기록 파이썬 아니에요 알고 있지만, 파이썬 c를로 변환하는 (어떤 문제 케이크 의 조각을 가지고 언어가 아닌 알고리즘입니다)