Mepụta ngwa ihe nchọgharị Weebụ na ngwa ndekọ desktọpụ site na iji PyGobject - Nkebi nke 3
Nke a bụ akụkụ 3 nke usoro gbasara imepụta ngwa GUI n'okpuru desktọpụ Linux site na iji PyGObject. Taa, anyị ga-ekwu maka iji ụfọdụ modul Python dị elu & ọba akwụkwọ n'ime mmemme anyị dị ka 'os', 'WebKit', 'arịrịọ' na ndị ọzọ, n'akụkụ ụfọdụ ozi bara uru maka mmemme.
Ị ga-agarịrị na akụkụ ndị a niile gara aga nke usoro a si ebe a, ka ịga n'ihu na ntuziaka ndị ọzọ maka ịmepụta ngwa ngwa ndị ọzọ:
- Mepụta ngwa GUI n'okpuru desktọpụ Linux Iji PyGObject - Nkebi 1
- Ịmepụta ngwa PyGobject Ọganihu na Linux – Nkebi nke 2
Modul & ọba akwụkwọ dị na Python bara ezigbo uru, kama ịde ọtụtụ mmemme iji rụọ ọrụ dị mgbagwoju anya nke ga-ewe oge na ọrụ, ị nwere ike ibubata ha! Ee, dị nnọọ ibubata modul & ọba akwụkwọ ịchọrọ na mmemme gị ma ị ga-enwe ike ịchekwa oge na mgbalị dị ukwuu iji mezue mmemme gị.
Enwere ọtụtụ modul ama ama maka Python, nke ị nwere ike ịhụ na Python Module Index.
Ị nwere ike ibubata ọba akwụkwọ nakwa maka mmemme Python gị, site na gi.repository import Gtk ahịrị a na-ebubata ọba akwụkwọ GTK na mmemme Python, enwere ọtụtụ ọba akwụkwọ ndị ọzọ dị ka Gdk, WebKit.. wdg.
Ịmepụta Ngwa GUI Ọganihu
Taa, anyị ga-emepụta mmemme 2:
- Ihe nchọgharị weebụ dị mfe; nke ga-eji ọbá akwụkwọ WebKit.
- Ihe ndekọ desktọpụ na-eji iwu 'avconv'; nke ga-eji modul ‘os’ sitere na Python.
Agaghị m akọwa otu esi adọta & dobe wijetị n'ime Glade mmebe site ugbu a gaa n'ihu, naanị m ga-agwa gị aha wijetị nke ịchọrọ ịmepụta, gbakwunyere m ga-enye gị > .glade faịlụ maka mmemme ọ bụla, yana faịlụ Python n'ezie.
Iji mepụta ihe nchọgharị weebụ, anyị ga-eji igwe \WebKit nke bụ igwe na-emepe emepe maka webụ, ọ bụ otu ihe a na-eji na >Chrome/Chromium, maka ozi ndị ọzọ gbasara ya ị nwere ike zoo aka na webụsaịtị Webkit.org gọọmentị.
Nke mbụ, anyị ga-emepụta GUI, mepee Glade mmebe wee tinye wijetị ndị a. Maka ozi ọzọ gbasara otu esi emepụta wijetị, soro Nkebi nke 1 na Nkebi nke 2 nke usoro isiokwu a (njikọ enyere n'elu).
- Mepụta wijetị 'window1'.
- Mepụta wijetị 'box1' na 'box2'.
- Mepụta 'bọtịnụ1' na 'bọtịnụ2' wijetị.
- Mepụta wijetị 'ntinye1'.
- Mepụta wijetị 'scrolledwindow1'.
Mgbe ịmepụta wijetị, ị ga-enweta interface ndị a.
Ọ dịghị ihe ọhụrụ, ma e wezụga wijetị \Orife Scrolled; wijetị a dị mkpa ka e nwee ike ịkụnye injin WebKit n'ime ya, na-eji \ > Window akpọgharịrị” wijetị ị ga-enwekwa ike pịgharịa gaa n'ihu na kwụ ọtọ ka ị na-eme nchọgharị na weebụsaịtị.
Ị ga-etinyezi \backbutton_clicked onye njikwa na bọtịnụ Azu \pịa signal, \refreshbutton_clicked b>” onye na-ahụ maka bọtịnụ ume ọhụrụ \pịa akara” na onye na-ahụ maka akara ngosi \arụ ọrụ maka ntinye .
Faịlụ .glade zuru ezu maka interface dị ebe a.
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.16.1 --> <interface> <requires lib="gtk+" version="3.10"/> <object class="GtkWindow" id="window1"> <property name="can_focus">False</property> <property name="title" translatable="yes">Our Simple Browser</property> <property name="window_position">center</property> <property name="default_width">1000</property> <property name="default_height">600</property> <property name="icon_name">applications-internet</property> <child> <object class="GtkBox" id="box1"> <property name="visible">True</property> <property name="can_focus">False</property> <property name="orientation">vertical</property> <child> <object class="GtkBox" id="box2"> <property name="visible">True</property> <property name="can_focus">False</property> <child> <object class="GtkButton" id="button1"> <property name="label">gtk-go-back</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="relief">half</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> <signal name="clicked" handler="backbutton_clicked" swapped="no"/> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkButton" id="button2"> <property name="label">gtk-refresh</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="relief">half</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> <signal name="clicked" handler="refreshbutton_clicked" swapped="no"/> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> <signal name="activate" handler="enterkey_clicked" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkScrolledWindow" id="scrolledwindow1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="hscrollbar_policy">always</property> <property name="shadow_type">in</property> <child> <placeholder/> </child> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> </object> </child> </object> </interface>
Ugbu a detuo koodu ahụ dị n'elu wee mado ya na faịlụ \ui.glade na nchekwa ụlọ gị. Ugbu a mepụta faịlụ ọhụrụ akpọrọ \mywebbrowser.py wee tinye ya. koodu na-esonụ n'ime ya, nkọwa niile dị na nkọwa.
#!/usr/bin/python # -*- coding: utf-8 -*- ## Here we imported both Gtk library and the WebKit engine. from gi.repository import Gtk, WebKit class Handler: def backbutton_clicked(self, button): ## When the user clicks on the Back button, the '.go_back()' method is activated, which will send the user to the previous page automatically, this method is part from the WebKit engine. browserholder.go_back() def refreshbutton_clicked(self, button): ## Same thing here, the '.reload()' method is activated when the 'Refresh' button is clicked. browserholder.reload() def enterkey_clicked(self, button): ## To load the URL automatically when the "Enter" key is hit from the keyboard while focusing on the entry box, we have to use the '.load_uri()' method and grab the URL from the entry box. browserholder.load_uri(urlentry.get_text()) ## Nothing new here.. We just imported the 'ui.glade' file. builder = Gtk.Builder() builder.add_from_file("ui.glade") builder.connect_signals(Handler()) window = builder.get_object("window1") ## Here's the new part.. We created a global object called 'browserholder' which will contain the WebKit rendering engine, and we set it to 'WebKit.WebView()' which is the default thing to do if you want to add a WebKit engine to your program. browserholder = WebKit.WebView() ## To disallow editing the webpage. browserholder.set_editable(False) ## The default URL to be loaded, we used the 'load_uri()' method. browserholder.load_uri("https://linux-console.net") urlentry = builder.get_object("entry1") urlentry.set_text("https://linux-console.net") ## Here we imported the scrolledwindow1 object from the ui.glade file. scrolled_window = builder.get_object("scrolledwindow1") ## We used the '.add()' method to add the 'browserholder' object to the scrolled window, which contains our WebKit browser. scrolled_window.add(browserholder) ## And finally, we showed the 'browserholder' object using the '.show()' method. browserholder.show() ## Give that developer a cookie ! window.connect("delete-event", Gtk.main_quit) window.show_all() Gtk.main()
Chekwaa faịlụ, wee mee ya.
$ chmod 755 mywebbrowser.py $ ./mywebbrowser.py
Ma nke a bụ ihe ị ga-enweta.
Ị nwere ike zoo aka maka akwụkwọ iwu WebKitGtk iji chọpụta nhọrọ ndị ọzọ.
Na ngalaba a, anyị ga-amụta ka esi agba ọsọ iwu sistemụ mpaghara ma ọ bụ shei scripts sitere na faịlụ Python site na iji 'osmodul, nke ga - enyere anyị aka ịmepụta ihe ndekọ ihuenyo dị mfe maka desktọpụ site na iji 'avconv' iwu.
Mepee onye nrụpụta Glade, wee mepụta wijetị ndị a:
- Mepụta wijetị 'window1'.
- Mepụta wijetị 'box1'.
- Mepụta 'bọtịnụ1', 'bọtịnụ2' na 'bọtịnụ3' wijetị.
- Mepụta wijetị 'ntinye1'.
Mgbe eke n'elu kwuru wijetị, ị ga-n'okpuru interface.
Nke a bụ faịlụ ui.glade zuru ezu.
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.16.1 --> <interface> <requires lib="gtk+" version="3.10"/> <object class="GtkWindow" id="window1"> <property name="can_focus">False</property> <property name="title" translatable="yes">Our Simple Recorder</property> <property name="window_position">center</property> <property name="default_width">300</property> <property name="default_height">30</property> <property name="icon_name">applications-multimedia</property> <child> <object class="GtkBox" id="box1"> <property name="visible">True</property> <property name="can_focus">False</property> <child> <object class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkButton" id="button1"> <property name="label">gtk-media-record</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> <signal name="clicked" handler="recordbutton" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkButton" id="button2"> <property name="label">gtk-media-stop</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> <signal name="clicked" handler="stopbutton" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> <child> <object class="GtkButton" id="button3"> <property name="label">gtk-media-play</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="use_stock">True</property> <property name="always_show_image">True</property> <signal name="clicked" handler="playbutton" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">3</property> </packing> </child> </object> </child> </object> </interface>
Dị ka ọ na-adị, detuo koodu ahụ dị n'elu wee mado ya na faịlụ \ui.glade na ndekọ ụlọ gị, mepụta faịlụ \myrecorder.py ọhụrụ wee tinye ya. koodu na-esonụ n'ime ya (A na-akọwa ahịrị ọhụrụ ọ bụla na nkọwa).
#!/usr/bin/python # -*- coding: utf-8 -*- ## Here we imported both Gtk library and the os module. from gi.repository import Gtk import os class Handler: def recordbutton(self, button): ## We defined a variable: 'filepathandname', we assigned the bash local variable '$HOME' to it + "/" + the file name from the text entry box. filepathandname = os.environ["HOME"] + "/" + entry.get_text() ## Here exported the 'filepathandname' variable from Python to the 'filename' variable in the shell. os.environ["filename"] = filepathandname ## Using 'os.system(COMMAND)' we can execute any shell command or shell script, here we executed the 'avconv' command to record the desktop video & audio. os.system("avconv -f x11grab -r 25 -s `xdpyinfo | grep 'dimensions:'|awk '{print $2}'` -i :0.0 -vcodec libx264 -threads 4 $filename -y & ") def stopbutton(self, button): ## Run the 'killall avconv' command when the stop button is clicked. os.system("killall avconv") def playbutton(self, button): ## Run the 'avplay' command in the shell to play the recorded file when the play button is clicked. os.system("avplay $filename &") ## Nothing new here.. We just imported the 'ui.glade' file. builder = Gtk.Builder() builder.add_from_file("ui.glade") builder.connect_signals(Handler()) window = builder.get_object("window1") entry = builder.get_object("entry1") entry.set_text("myrecording-file.avi") ## Give that developer a cookie ! window.connect("delete-event", Gtk.main_quit) window.show_all() Gtk.main()
Ugbu a mee faịlụ ahụ site na itinye iwu ndị a na njedebe.
$ chmod 755 myrecorder.py $ ./myrecorder.py
Na ị nwetara mbụ gị na-edekọ desktọpụ.
Ị nwere ike ịchọta ozi ndị ọzọ gbasara modul ''os' na Python OS Library.
Ma nke ahụ bụ ya, ịmepụta ngwa maka desktọpụ Linux adịghị esiri ike iji PyGObject, naanị ị ga-emepụta GUI, bubata ụfọdụ modul ma jikọta faịlụ Python na GUI, ọ dịghị ihe ọzọ, ọ dịghị obere. Enwere ọtụtụ nkuzi bara uru gbasara ime nke a na webụsaịtị PyGObject:
Ị nwara ịmepụta ngwa site na iji PyGObject? Kedu ihe ị chere maka ime otú ahụ? Kedu ngwa ị mepụtara na mbụ?