Unda Kivinjari Chako Mwenyewe cha Wavuti na Programu za Rekoda ya Eneo-kazi Ukitumia PyGobject - Sehemu ya 3


Hii ni sehemu ya 3 ya mfululizo kuhusu kuunda programu za GUI chini ya eneo-kazi la Linux kwa kutumia PyGObject. Leo tutazungumza kuhusu kutumia moduli za kina za Python na maktaba katika programu zetu kama vile 'os', 'WebKit', 'maombi' na wengine, kando na habari zingine muhimu za upangaji programu.

Ni lazima upitie sehemu hizi zote za awali za mfululizo kutoka hapa, ili kuendelea na maagizo zaidi ya kuunda programu mapema zaidi:

  1. Unda Programu za GUI Chini ya Eneo-kazi la Linux Ukitumia PyGObject - Sehemu ya 1
  2. Kuunda Programu za Advance PyGobject kwenye Linux - Sehemu ya 2

Moduli na maktaba katika Python ni muhimu sana, badala ya kuandika programu-ndogo nyingi kufanya kazi ngumu ambazo zitachukua muda mwingi na kazi, unaweza kuziagiza tu! Ndiyo, ingiza tu moduli na maktaba unayohitaji kwenye programu yako na utaweza kuokoa muda na juhudi nyingi ili kukamilisha programu yako.

Kuna moduli nyingi maarufu za Python, ambazo unaweza kupata kwenye Python Module Index.

Unaweza kuleta maktaba pia kwa ajili ya programu yako ya Python, kutoka kwa “gi.repository import Gtk” laini hii inaleta maktaba ya GTK kwenye mpango wa Python, kuna maktaba nyingine nyingi kama Gdk, WebKit.. n.k.

Kuunda Programu za Advance GUI

Leo, tutaunda programu 2:

  1. Kivinjari rahisi; ambayo itatumia maktaba ya WebKit.
  2. Rekoda ya eneo-kazi kwa kutumia amri ya ‘avconv’; ambayo itatumia moduli ya ‘os’ kutoka Python.

Sitaeleza jinsi ya kuburuta na kudondosha wijeti katika mtengenezaji wa Glade kuanzia sasa, nitakuambia tu jina la wijeti ambazo unahitaji kuunda, zaidi ya hayo nitakupa .gladefaili kwa kila programu, na faili ya Python kwa hakika.

Ili kuunda kivinjari cha wavuti, itabidi tutumie injini ya \WebKit”, ambayo ni injini ya uwasilishaji ya chanzo huria ya wavuti, ni ile ile inayotumika katika Chrome/Chromium, kwa maelezo zaidi kuihusu unaweza kurejelea tovuti rasmi ya Webkit.org.

Kwanza, itabidi tuunde GUI, tufungue kiunda Glade na kuongeza wijeti zifuatazo. Kwa maelezo zaidi kuhusu jinsi ya kuunda wijeti, fuata Sehemu ya 1 na Sehemu ya 2 ya mfululizo huu (viungo vilivyotolewa hapo juu).

  1. Unda wijeti ya ‘window1’.
  2. Unda wijeti ya ‘box1’ na ‘box2’.
  3. Unda wijeti ya ‘button1’ na ‘button2’.
  4. Unda wijeti ya ‘entry1’.
  5. Unda wijeti ya ‘scrolledwindow1’.

Baada ya kuunda vilivyoandikwa, utapata kiolesura kifuatacho.

Hakuna jipya, isipokuwa wijeti ya \Dirisha Lililovingirishwa; wijeti hii ni muhimu ili kuruhusu injini ya WebKit kupandikizwa ndani yake, kwa kutumia \Dirisha Iliyovingirishwa” wijeti utaweza pia kusogeza kwa usawa na wima unapovinjari tovuti.

Sasa itabidi uongeze \kidhibiti_kilichobofyewa nyuma” kwenye kitufe cha Nyuma \kilichobofya, \kibofya upya_kibofya b>” kidhibiti kwenye kitufe cha Onyesha upya \mawimbi uliyobofya” na \imebofya_ingiza_iliyobofya” kidhibiti kwa ishara ya \iliyoamilishwa kwa ingizo. .

Faili kamili ya .glade ya kiolesura iko hapa.

<?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>

Sasa nakili msimbo ulio hapo juu na ubandike katika \ui.glade faili katika folda yako ya nyumbani. Sasa unda faili mpya inayoitwa \mywebrowser.py na uingize nambari ifuatayo ndani yake, maelezo yote yako kwenye maoni.

#!/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()

Hifadhi faili, na uikimbie.

$ chmod 755 mywebbrowser.py
$ ./mywebbrowser.py

Na hii ndio utapata.

Unaweza kurejelea WebKitGtk hati rasmi ili kugundua chaguo zaidi.

Katika sehemu hii, tutajifunza jinsi ya kuendesha amri za mfumo wa ndani au hati za ganda kutoka kwa faili ya Python kwa kutumia moduli ya 'os', ambayo itatusaidia kuunda kirekodi skrini rahisi kwa eneo-kazi kwa kutumia 'avconv' amri.

Fungua mbuni wa Glade, na uunde wijeti zifuatazo:

  1. Unda wijeti ya ‘window1’.
  2. Unda wijeti ya ‘box1’.
  3. Unda wijeti za ‘button1’, ‘button2’ na ‘button3’.
  4. Unda wijeti ya ‘entry1’.

Baada ya kuunda vilivyoandikwa hapo juu, utapata kiolesura cha chini.

Hii hapa ni faili kamili ya 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>

Kama kawaida, nakili msimbo ulio hapo juu na ubandike kwenye faili \ui.glade” katika saraka yako ya nyumbani, unda faili mpya \myrecorder.py na uingize nambari ifuatayo ndani yake (Kila mstari mpya umeelezewa kwenye maoni).

#!/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()

Sasa endesha faili kwa kutumia amri zifuatazo kwenye terminal.

$ chmod 755 myrecorder.py
$ ./myrecorder.py

Na umepata kinasa sauti chako cha kwanza cha eneo-kazi.

Unaweza kupata maelezo zaidi kuhusu moduli ya ‘os’ kwenye Maktaba ya Python OS.

Na ndivyo ilivyo, kuunda programu kwa desktop ya Linux si vigumu kutumia PyGObject, unapaswa tu kuunda GUI, kuagiza baadhi ya modules na kuunganisha faili ya Python na GUI, hakuna zaidi, hakuna chini. Kuna mafunzo mengi muhimu kuhusu kufanya hivi kwenye wavuti ya PyGObject:

Umejaribu kuunda programu kwa kutumia PyGObject? Una maoni gani kuhusu kufanya hivyo? Je, umetengeneza programu gani hapo awali?