ສ້າງໂປແກຼມທ່ອງເວັບຂອງຕົວເອງແລະໂປແກຼມບັນທຶກ desktop ຂອງທ່ານໂດຍໃຊ້ PyGobject - ພາກທີ 3
ນີ້ແມ່ນສ່ວນ ທີ 3 ຂອງຊຸດກ່ຽວກັບການສ້າງໂປແກຼມ GUI ພາຍໃຕ້ Linux desktop ໂດຍໃຊ້ PyGObject. ມື້ນີ້ພວກເຮົາຈະເວົ້າກ່ຽວກັບການ ນຳ ໃຊ້ໂມດູນ Python ຂັ້ນສູງ ຈຳ ນວນ ໜຶ່ງ ໃນໂປແກຼມຂອງພວກເຮົາເຊັ່ນ ' os ', ' WebKit ', ' ຄຳ ຮ້ອງຂໍ ແລະ ອື່ນໆ, ນອກ ເໜືອ ຈາກບາງຂໍ້ມູນທີ່ເປັນປະໂຫຍດ ສຳ ລັບການຂຽນໂປແກຼມ.
ທ່ານຕ້ອງຜ່ານພາກສ່ວນທີ່ຜ່ານມາທັງ ໝົດ ຂອງຊຸດນີ້ຈາກບ່ອນນີ້, ເພື່ອສືບຕໍ່ ຄຳ ແນະ ນຳ ເພີ່ມເຕີມກ່ຽວກັບການສ້າງ ຄຳ ຮ້ອງສະ ໝັກ ລ່ວງ ໜ້າ ເພີ່ມເຕີມ:
<
ໂມດູນແລະຫ້ອງສະ ໝຸດ ຕ່າງໆໃນ Python ແມ່ນມີປະໂຫຍດຫຼາຍ, ແທນທີ່ຈະຂຽນໂປແກຼມຍ່ອຍຫຼາຍໆຢ່າງເພື່ອເຮັດວຽກທີ່ສັບສົນບາງຢ່າງເຊິ່ງຈະໃຊ້ເວລາແລະວຽກຫຼາຍ, ທ່ານພຽງແຕ່ສາມາດ ນຳ ເຂົ້າພວກມັນໄດ້! ແມ່ນແລ້ວ, ພຽງແຕ່ ນຳ ເຂົ້າໂມດູນແລະຫ້ອງສະ ໝຸດ ທີ່ທ່ານຕ້ອງການເຂົ້າໃນໂປແກຼມຂອງທ່ານແລະທ່ານຈະສາມາດປະຫຍັດເວລາແລະຄວາມພະຍາຍາມຫຼາຍເພື່ອເຮັດ ສຳ ເລັດໂຄງການຂອງທ່ານ.
ມີຫລາຍໂມດູນທີ່ມີຊື່ສຽງ ສຳ ລັບ Python, ເຊິ່ງທ່ານສາມາດຊອກຫາໄດ້ຢູ່ທີ່ Python Module Module.
ທ່ານສາມາດ ນຳ ເຂົ້າຫໍສະ ໝຸດ ເຊັ່ນດຽວກັນ ສຳ ລັບໂປແກຼມ Python ຂອງທ່ານ, ຈາກ " gi.repository import Gtk " ເສັ້ນນີ້ ນຳ ເຂົ້າຫ້ອງສະ ໝຸດ GTK ເຂົ້າໃນໂປແກຼມ Python, ມັນມີຫລາຍຫ້ອງສະ ໝຸດ ອື່ນໆເຊັ່ນ Gdk, WebKit .. .
ການສ້າງໂປແກຼມ GUI ລ່ວງ ໜ້າ
ມື້ນີ້, ພວກເຮົາຈະສ້າງ 2 ໂປແກຼມ:
<
ຂ້ອຍຈະບໍ່ອະທິບາຍວິທີການລາກແລະວາງ widget ໃນຜູ້ອອກແບບ Glade ຕັ້ງແຕ່ນີ້, ຂ້ອຍພຽງແຕ່ຈະບອກຊື່ຂອງເຄື່ອງມືທີ່ເຈົ້າຕ້ອງສ້າງ, ນອກຈາກນັ້ນຂ້ອຍຈະໃຫ້ .glade file ສຳ ລັບແຕ່ລະໂປແກຼມ, ແລະເອກະສານ Python ແນ່ນອນ.
ເພື່ອສ້າງໂປແກຼມທ່ອງເວັບ, ພວກເຮົາຈະຕ້ອງໃຊ້ເຄື່ອງຈັກ " WebKit ", ເຊິ່ງແມ່ນເຄື່ອງຈັກ ສຳ ລັບເປີດເວັບ ສຳ ລັບເວັບ, ມັນກໍ່ຄືກັນກັບເຄື່ອງຈັກທີ່ໃຊ້ໃນ Chrome / Chromium , ສຳ ລັບຂໍ້ມູນເພີ່ມເຕີມທ່ານສາມາດອ້າງອີງເຖິງເວັບໄຊທ໌ທາງການຂອງ Webkit.org.
ກ່ອນອື່ນ ໝົດ, ພວກເຮົາຈະຕ້ອງສ້າງ GUI, ເປີດຜູ້ອອກແບບ Glade ແລະເພີ່ມ widget ຕໍ່ໄປນີ້. ສຳ ລັບຂໍ້ມູນເພີ່ມເຕີມກ່ຽວກັບວິທີການສ້າງເຄື່ອງມື, ໃຫ້ຕິດຕາມ ສ່ວນທີ 1 ແລະ ພາກທີ 2 ຂອງຊຸດນີ້ (ລິ້ງທີ່ໃຫ້ໄວ້ຂ້າງເທິງ)
<
ຫຼັງຈາກການສ້າງ widget, ທ່ານຈະໄດ້ຮັບການໂຕ້ຕອບຕໍ່ໄປນີ້.
ບໍ່ມີຫຍັງ ໃໝ່, ຍົກເວັ້ນບັນດາ " ປ່ອງຢ້ຽມເລື່ອນ "; widget ນີ້ແມ່ນມີຄວາມ ສຳ ຄັນເພື່ອໃຫ້ເຄື່ອງຈັກ WebKit ຖືກຝັງຢູ່ໃນມັນ, ໂດຍໃຊ້ " ເລື່ອນ ໜ້າ ຈໍ " ທ່ານຍັງສາມາດເລື່ອນອອກທາງນອນແລະແນວນອນໄດ້ໃນຂະນະທີ່ທ່ານ ທ່ອງເວັບໄຊທ໌ຕ່າງໆ.
ດຽວນີ້ທ່ານຈະຕ້ອງເພີ່ມເຄື່ອງມືຈັດການ“ backbutton_clicked ” ໃສ່ປຸ່ມ ກັບຄືນໄປບ່ອນ ທີ່ຢູ່ສັນຍາລັກ“ ກົດປຸ່ມ ”,“ ໂຫຼດຄືນບູຊາ ” ຜູ້ຈັດການກັບປຸ່ມ Refresh“ ສັນຍານທີ່ຖືກຄລິກ ” ແລະຜູ້ຈັດການກັບເຄື່ອງມື“ ສັນຍາລັກ” ເປີດໃຊ້ ” ສຳ ລັບການເຂົ້າ.
ເອກະສານທີ່ . ລຽບ ສຳ ລັບການໂຕ້ຕອບແມ່ນຢູ່ທີ່ນີ້.
<?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>
ດຽວນີ້ ສຳ ເນົາລະຫັດຂ້າງເທິງແລ້ວເອົາມັນລົງໃນ“ ui.glade ” ໃນແຟ້ມເຮືອນຂອງທ່ານ. ຕອນນີ້ສ້າງແຟ້ມເອກະສານ ໃໝ່ ທີ່ມີຊື່ວ່າ " mywebbrowser.py " ແລະໃສ່ລະຫັດຕໍ່ໄປນີ້ພາຍໃນມັນ, ຄຳ ອະທິບາຍທັງ ໝົດ ແມ່ນຢູ່ໃນ ຄຳ ເຫັນ.
#!/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()
ບັນທຶກເອກະສານ, ແລະແລ່ນມັນ.
$ chmod 755 mywebbrowser.py $ ./mywebbrowser.py
ແລະນີ້ແມ່ນສິ່ງທີ່ທ່ານຈະໄດ້ຮັບ.
ທ່ານອາດຈະອ້າງອີງໃສ່ເອກະສານທາງການ WebKitGtk ເພື່ອຄົ້ນຫາຕົວເລືອກເພີ່ມເຕີມ.
ໃນພາກນີ້, ພວກເຮົາຈະຮຽນຮູ້ວິທີການ ດຳ ເນີນການ ຄຳ ສັ່ງລະບົບທ້ອງຖິ່ນຫລື shell scripts ຈາກໄຟລ໌ Python ໂດຍໃຊ້ໂມດູນ ' os ເຊິ່ງຈະຊ່ວຍໃຫ້ພວກເຮົາສ້າງເຄື່ອງບັນທຶກ ໜ້າ ຈໍແບບ ທຳ ມະດາ ສຳ ລັບ desktop ໂດຍໃຊ້ ຄຳ ສັ່ງ ' avconv '.
ເປີດຜູ້ອອກແບບ Glade, ແລະສ້າງ widget ຕໍ່ໄປນີ້:
<
ຫຼັງຈາກການສ້າງ widget ທີ່ກ່າວມາຂ້າງເທິງ, ທ່ານຈະໄດ້ຮັບການໂຕ້ຕອບຂ້າງລຸ່ມນີ້.
ນີ້ແມ່ນເອກະສານ ui.glade ທີ່ສົມບູນແລ້ວ.
<?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>
ຕາມປົກກະຕິ, ສຳ ເນົາຂໍ້ ກຳ ນົດຂ້າງເທິງນີ້ແລ້ວເອົາມັນລົງໃນເອກະສານ“ ui.glade ” ໃນບັນຊີລາຍຊື່ບ້ານຂອງທ່ານ, ສ້າງແຟ້ມ“ myrecorder.py ” ໃໝ່ ແລະໃສ່ດັ່ງຕໍ່ໄປນີ້ ລະຫັດພາຍໃນມັນ (ທຸກໆເສັ້ນ ໃໝ່ ແມ່ນອະທິບາຍໃນ ຄຳ ເຫັນ).
#!/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()
ໃນປັດຈຸບັນດໍາເນີນການໄຟລ໌ໂດຍການນໍາໃຊ້ຄໍາສັ່ງດັ່ງຕໍ່ໄປນີ້ຢູ່ໃນປາຍ.
$ chmod 755 myrecorder.py $ ./myrecorder.py
ແລະທ່ານໄດ້ຮັບເຄື່ອງບັນທຶກ desktop ຄັ້ງ ທຳ ອິດຂອງທ່ານ.
ທ່ານສາມາດຊອກຫາຂໍ້ມູນເພີ່ມເຕີມກ່ຽວກັບໂມດູນ ‘ os ‘ ທີ່ Python OS Library.
ແລະມັນກໍ່ແມ່ນມັນ, ການສ້າງໂປແກຼມ ສຳ ລັບ desktop Linux ບໍ່ຍາກທີ່ຈະໃຊ້ PyGObject , ທ່ານພຽງແຕ່ຕ້ອງສ້າງ GUI, ນຳ ເຂົ້າບາງໂມດູນແລະເຊື່ອມໂຍງເອກະສານ Python ກັບ GUI, ບໍ່ມີຫຍັງອີກ, ບໍ່ມີຫຍັງ ໜ້ອຍ ກວ່າ. ມີການສອນທີ່ມີປະໂຫຍດຫຼາຍຢ່າງກ່ຽວກັບການເຮັດສິ່ງນີ້ໃນເວບໄຊທ໌ PyGObject:
ທ່ານໄດ້ພະຍາຍາມສ້າງໂປແກຼມຕ່າງໆໂດຍໃຊ້ PyGObject ບໍ? ທ່ານຄິດແນວໃດກ່ຽວກັບການເຮັດເຊັ່ນນັ້ນ? ທ່ານໄດ້ພັດທະນາໂປແກຼມຫຍັງກ່ອນ?