Your SlideShare is downloading. ×
0
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Python-GTK
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Python-GTK

5,020

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,020
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
130
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Yuren Ju <yurenju@gmail.com> Python-GTK
  • 2. Yuren Ju <ul><li>Yuren&apos;s Info Area - http://yureinfore.blogspot.com/
  • 3. Yuren Ju – Linkedin http://tw.linkedin.com/in/yurenju </li></ul>
  • 4. 本份投影片…… <ul><li>假設你學過任何一種程式語言 </li><ul><li>特別有可能是Java </li></ul></ul>
  • 5. + Python GTK+
  • 6. &nbsp;
  • 7. Source: TIOBE Software index TIOBE 程式語言社群指標
  • 8. ? Source: &amp;quot;Why?&amp;quot;, Mike Luckovich, Pulitzer-Winning Political Cartoonist (1 of 4) , cc-by-deed
  • 9. 別人怎麼說… <ul><li>適合初學者,但也適合進階開發者
  • 10. 高度彈性:從大型專案到小型專案都適合使用
  • 11. you can get the job done </li></ul>Source: What is Python and Why Python
  • 12. 成功案例
  • 13. 成功案例 <ul><li>三年前……
  • 14. Python 初體驗 - 好吃不黏牙!
  • 15. 要找個簡單的語言寫作業
  • 16. Perl? PHP? Java? </li><ul><li>Perl: 學過了,想換個口味
  • 17. PHP: 在網頁上寫很麻煩,直接寫成script感覺又有點怪
  • 18. Java: 程式碼超長 </li><ul><li>Print: System.out.println
  • 19. 每個程式都一定要宣告Class </li></ul></ul></ul>
  • 20. &nbsp;
  • 21. 對我來說的Python優點 <ul><li>短小精簡
  • 22. 快速驗證想法 </li></ul>
  • 23. Windows Form wxWidgets Qt GTK+ Swing/AWT Android View/Widget/Layout MFC
  • 24. &nbsp;
  • 25. Cross Platform crossing the line , Jkönig, CC BY-NC-SA 2.0
  • 26. GTK+ Python C/C++ Perl Ruby C# ... Linux Windows Mac PHP
  • 27. 使用GTK的軟/硬體 Nokia n900
  • 28. Python
  • 29. Start! 將程式碼寫於檔案中 打開終端機直接鍵入程式
  • 30. 打開你的終端機! $ python
  • 31. python command-line
  • 32. print &amp;quot;你好世界!&amp;quot;
  • 33. 數值指定 <ul><li>String </li><ul><li>var = &amp;quot;this is String&amp;quot; </li></ul><li>Integer </li><ul><li>var = 1 </li></ul></ul><ul><li>Boolean </li><ul><li>var = True </li></ul><li>Float </li><ul><li>var = 0.1 </li></ul></ul>不需要宣告形態 String var = &amp;quot;this is String&amp;quot;; int var = 1; boolean var = true; float var = 0.1;
  • 34. Python鐵則 <ul><li>if/else不用括號
  • 35. 每行結尾不用分號
  • 36. 縮排
  • 37. 冒號 </li></ul>
  • 38. Example: agent smith if first == True : print &amp;quot;Mr. Anderson .&amp;quot; else : print &amp;quot;Welcome back, we missed you.&amp;quot; if (first == true) { System.out.println (&amp;quot;Mr. Anderson.&amp;quot;); } else { System.out.println (&amp;quot;Welcome back, we missed you.&amp;quot;); }
  • 39. if first == True : print &amp;quot;Mr. Anderson .&amp;quot; else : print &amp;quot;Welcome back, we missed you.&amp;quot; 4 個空白或是一個 tab 開頭原本是用括號的地方改用冒號 結尾不需要括號
  • 40. list &amp; dict <ul><li>list =&gt; 陣列
  • 41. dict =&gt; hash table </li></ul>
  • 42. list <ul><li>actors = [&amp;quot;Neo&amp;quot;, &amp;quot;Smith&amp;quot;, &amp;quot;Trinity&amp;quot;, &amp;quot;Oracle&amp;quot;]
  • 43. mixed = [&amp;quot;hi&amp;quot;, &amp;quot;apple&amp;quot;, 0, 0.1, False]
  • 44. print actors[1]
  • 45. actors.append (&amp;quot;Morpheus&amp;quot;)
  • 46. del actors[1]
  • 47. actors.remove (&amp;quot;Oracle&amp;quot;) </li></ul>
  • 48. list (cont.) for a in actors: print a for a in actors[2:]: print a print sorted (actors) del actors[2:]
  • 49. dict person = {&amp;quot;name&amp;quot;: &amp;quot;Yuren Ju&amp;quot;, &amp;quot;website&amp;quot;: &amp;quot;http://yure...&amp;quot;, &amp;quot;birthday&amp;quot;: &amp;quot;yyyy/mm/dd&amp;quot;} print person[&amp;quot;name&amp;quot;] print person[&amp;quot;website&amp;quot;]
  • 50. dict for k in person: print &amp;quot;%s: %s&amp;quot; % (k, person[k]) if person.has_key (&amp;quot;age&amp;quot;): print &amp;quot;no age attribute&amp;quot;
  • 51. function def func (arg1, arg2): #Do something... return ret
  • 52. Editor 將程式碼寫於檔案中
  • 53. 建議 <ul><li>Geany
  • 54. Gedit
  • 55. eclipse
  • 56. Of cause~ vim! </li></ul>
  • 57. Hello-world.py # -*- coding: utf -8 -*- print &amp;quot; 你好世界! &amp;quot;
  • 58. line 1 預設 # -*- coding: utf-8 -*- Python String Byte String Unicode String
  • 59. gedit
  • 60. 實戰?
  • 61. http://ajax.googleapis.com/ajax/services/language/translate? v=1.0&amp;ie=utf8&amp;q=test&amp;langpair=en|zh-tw http://j.mp/dXYwLT
  • 62. #!/usr/bin/env python # -*- coding: utf-8 -*- from urllib2 import urlopen from urllib import urlencode import simplejson import sys def translate (text): sl= &amp;quot;zh-tw&amp;quot; tl= &amp;quot;en&amp;quot; langpair= &apos;%s|%s&apos; %(tl,sl) base_url = &apos;http://ajax.googleapis.com/ajax/services/language/translate?&apos; data = urlencode({ &apos;v&apos; : 1.0 , &apos;ie&apos; : &apos;UTF8&apos; , &apos;q&apos; : text, &apos;langpair&apos; :langpair}) url = base_url+data urlres = urlopen(url) json = simplejson.loads(urlres.read()) result = json[ &apos;responseData&apos; ][ &apos;translatedText&apos; ] return result if __name__ == &amp;quot;__main__&amp;quot; : print translate (sys.argv[ 1 ])
  • 63. Class/Object #!/usr/bin/env python # -*- coding: utf-8 -*- class FirstClass : def __init__ (self): self.data = [ &amp;quot;a&amp;quot; , &amp;quot;b&amp;quot; , &amp;quot;c&amp;quot; ] def print_data (self): print self.data if __name__ == &amp;quot;__main__&amp;quot; : f = FirstClass() f.print_data()
  • 64. GTK+
  • 65. #!/usr/bin/env python import pygtk pygtk.require ( &apos;2.0&apos; ) import gtk if __name__ == &amp;quot;__main__&amp;quot; : window = gtk.Window (); window.show () gtk.main ()
  • 66. Event Handler
  • 67. #!/usr/bin/env python import pygtk pygtk.require ( &apos;2.0&apos; ) import gtk def destroy (window): gtk.main_quit () def hello (button): print &amp;quot;Hello World&amp;quot; if __name__ == &amp;quot;__main__&amp;quot; : window = gtk.Window () window.connect ( &amp;quot;destroy&amp;quot; , destroy) window.show () button = gtk.Button ( &amp;quot;hello&amp;quot; ); button.connect ( &amp;quot;clicked&amp;quot; , hello) button.show () window.add (button) gtk.main ()
  • 68. #!/usr/bin/env python import pygtk pygtk.require ( &apos;2.0&apos; ) import gtk class HelloWorld : def __init__ (self): window = gtk.Window () window.connect ( &amp;quot;destroy&amp;quot; , self.destroy) window.show () button = gtk.Button ( &amp;quot;hello&amp;quot; ); button.connect ( &amp;quot;clicked&amp;quot; , self.hello) button.show () window.add (button) self.position = 0 def destroy (self, window): gtk.main_quit () def hello (self, button): print &amp;quot;Hello World, position: %d&amp;quot; % self.position self.position += 1 if __name__ == &amp;quot;__main__&amp;quot; : hello = HelloWorld () gtk.main () Keep state, using class/object
  • 69. Layout. layouts , Kemeny_x, CC BY-NC 2.0
  • 70. GTK+ Layout – box packing
  • 71. HBox VBox
  • 72. &nbsp;
  • 73. HBox VBox VBox
  • 74. hbox = gtk.HBox() vbox1 = gtk.VBox() vbox2 = gtk.VBox() hbox.pack_start(vbox1) hbox.pack_start(vbox2) pack_start / pack_end
  • 75. window = gtk.Window () hbox = gtk.HBox() vbox1 = gtk.VBox() vbox2 = gtk.VBox() hbox.pack_start(vbox1) hbox.pack_start(vbox2) label = gtk.Label (&amp;quot;Text: &amp;quot;) vbox1.pack_start (label) textview = gtk.TextView () vbox1.pack_start (textview) button_ok = gtk.Button (&amp;quot;OK&amp;quot;) vbox2.pack_end (button_ok) button_cancel = gtk.Button (&amp;quot;Cancel&amp;quot;) vbox2.pack_end (button_cancel) hbox.show_all() window.add(hbox) window.show()
  • 76. layout expand=True fill=True expand=True fill=False expand=False fill=False
  • 77. window = gtk.Window () hbox = gtk.HBox() vbox1 = gtk.VBox() vbox2 = gtk.VBox() hbox.pack_start(vbox1) hbox.pack_start(vbox2, expand=False) label = gtk.Label (&amp;quot;Text: &amp;quot;) label.set_property(&apos;xalign&apos;, 0.0) textview = gtk.TextView () vbox1.pack_start (label, expand=False) vbox1.pack_start (textview) button_ok = gtk.Button (&amp;quot;OK&amp;quot;) button_cancel = gtk.Button (&amp;quot;Cancel&amp;quot;) vbox2.pack_end (button_ok, expand=False) vbox2.pack_end (button_cancel, expand=False) hbox.show_all() window.add(hbox) window.show()
  • 78. Gtk Builder
  • 79. Gtk Builder builder = gtk.Builder () builder.add_from_file (&amp;quot;layout.glade&amp;quot;) window = builder.get_object (&amp;quot;window1&amp;quot;) window.show ()
  • 80. 翻譯軟體 – GTK 版本 <ul><li>拉UI (glade)
  • 81. 事件分配 (glade)
  • 82. 事件分配 (python) </li></ul>
  • 83. UI Freeze Frozen Moment , drinksmachine, by-nc-nd
  • 84. English 中文 UI Freeze
  • 85. UI Freeze 費時的操作
  • 86. Thread 嘿 Neo, 又是我
  • 87. UI update 費時的操作 Thread
  • 88. def translate (text): ... ... class TransThread (Thread): def __init__ (self, text, obj): self.text = text Thread.__init__(self) def run (self): try : self.result = translate(self.text) except : self.result = &amp;quot;error&amp;quot; Thread
  • 89. Communication? ? UI part Thread part
  • 90. 概念 <ul><li>註冊一個信號(signal)
  • 91. 連接此signal
  • 92. 當Thread結束後射出(emit)此signal </li></ul>gtk-gtr2.py
  • 93. 範例
  • 94. TreeView TreeViewColumn CellRendererText ListStore TreeStore View Model
  • 95. Glade ListStore
  • 96. Weather CitiesThread Create WeatherThread Create 產生城市列表 取得城市天氣 load-cities-completed load-weather-completed __init__ selected_city
  • 97. def __init__ (self): self.__gobject_init__() self.builder = gtk.Builder() self.builder.add_from_file ( &amp;quot;weather.glade&amp;quot; ) self.builder.connect_signals(self) win = self.builder.get_object( &amp;quot;window1&amp;quot; ) win.show_all() self.register_signals() self.cities_thread = CitiesThread (self) self.cities_thread.start() self.tree = self.builder.get_object( &amp;quot;treeview_cities&amp;quot; ) col_name = gtk.TreeViewColumn( &amp;quot;city name&amp;quot; ) self.tree.append_column(col_name) cell = gtk.CellRendererText() col_name.pack_start(cell, True ) col_name.add_attribute(cell, &apos;text&apos; , 0 ) self.tree.connect( &amp;quot;cursor-changed&amp;quot; , self.selected_city) __init__
  • 98. class CitiesThread (Thread): def __init__ (self, obj): self.obj = obj Thread.__init__(self) def run (self): self.cities = pywapi. get_cities_from_google ( &apos;tw&apos; , &apos;zh-tw&apos; ) gtk.gdk.threads_enter() self.obj.emit( &amp;quot;load-cities-completed&amp;quot; ) gtk.gdk.threads_leave() CitiesThread
  • 99. WeatherThread class WeatherThread (Thread): def __init__ (self, obj, latitude, longitude): self.obj = obj self.latitude = latitude self.longitude = longitude Thread.__init__(self) def run (self): weather = pywapi.get_weather_from_google( &apos;,,,%s,%s&apos; % (self.latitude, self.longitude), &apos;zh-tw&apos; ) self.weather = { &amp;quot;condition&amp;quot; : weather[ &apos;current_conditions&apos; ][ &apos;condition&apos; ], &amp;quot;humidity&amp;quot; : weather[ &apos;current_conditions&apos; ][ &apos;humidity&apos; ], &amp;quot;temp_c&amp;quot; : weather[ &apos;current_conditions&apos; ][ &apos;temp_c&apos; ]} gtk.gdk.threads_enter() self.obj.emit( &amp;quot;load-weather-completed&amp;quot; ) gtk.gdk.threads_leave()
  • 100. load-cities-completed def load_cities_completed (self, obj): self.cities = self.cities_thread.cities self.liststore = self.builder.get_object( &amp;quot;cities&amp;quot; ) for city in self.cities: self.liststore.append ([city[ &apos;name&apos; ], long(city[ &apos;latitude_e6&apos; ]), long(city[ &apos;longitude_e6&apos; ])])
  • 101. load-weather-completed def load_weather_completed (self, obj): weather = self.weather_thread.weather self.builder.get_object( &amp;quot;label_temperature&amp;quot; ) .set_markup ( &amp;quot;&lt;span size=&apos;xx-large&apos;&gt; 溫度: %s&lt;/span&gt;&amp;quot; % weather[ &apos;temp_c&apos; ]) self.builder.get_object( &amp;quot;label_current&amp;quot; ) .set_label ( &amp;quot; 現在天氣: %s&amp;quot; % weather[ &apos;condition&apos; ]) self.builder.get_object( &amp;quot;label_humidity&amp;quot; ) .set_label ( &amp;quot; 濕度: %s&amp;quot; % weather[ &apos;humidity&apos; ])
  • 102. selected_city def selected_city (self, tree): selection = self.tree.get_selection() ( model , iter) = selection.get_selected() name = model.get_value(iter, 0 ) latitude = model.get_value(iter, 1 ) longitude = model.get_value(iter, 2 ) print &amp;quot;%s (%s, %s)&amp;quot; % (name, latitude, longitude) self.weather_thread = WeatherThread(self, latitude, longitude) self.weather_thread.start()
  • 103. gtk-weather.py https://gist.github.com/800513
  • 104. 釣竿 <ul><li>Dive Into Python 中文版
  • 105. PyGTK 2.0 Tutorial
  • 106. PyGTK 2.0 Reference Manual
  • 107. google &amp;quot;python gtk &lt;問題關鍵字&gt;&amp;quot;
  • 108. 在stackoverflow.com上面找答案 </li></ul>

×