Ƙirƙirar Mai Rarraba Gidan Gidan Gidan Gidanku da Aikace-aikacen Rikodi na Desktop Ta Amfani da PyGobject - Part 3


Wannan shine ɓangaren 3rd na jerin game da ƙirƙirar aikace-aikacen GUI a ƙarƙashin tebur na Linux ta amfani da PyGObject. A yau za mu yi magana game da amfani da wasu ci-gaba na kayan aikin Python & dakunan karatu a cikin shirye-shiryen mu kamar 'os', 'WebKit', 'buƙatun' da kuma wasu, ban da wasu bayanai masu amfani ga shirye-shirye.

Dole ne ku bi duk waɗannan sassan da suka gabata na jerin daga nan, don ci gaba da ƙarin umarni kan ƙirƙirar ƙarin aikace-aikacen gaba:

  1. Ƙirƙiri aikace-aikacen GUI ƙarƙashin Linux Desktop Ta amfani da PyGObject - Part 1
  2. Ƙirƙirar Aikace-aikacen PyGobject na Ci gaba akan Linux - Kashi na 2

Modules & dakunan karatu a Python suna da fa'ida sosai, maimakon rubuta shirye-shirye da yawa don yin wasu ayyuka masu rikitarwa waɗanda zasu ɗauki lokaci mai yawa da aiki, kawai kuna iya shigo da su! Ee, kawai shigo da kayayyaki da ɗakunan karatu da kuke buƙata zuwa shirin ku kuma zaku sami damar adana lokaci da ƙoƙari mai yawa don kammala shirin ku.

Akwai shahararrun samfuran Python da yawa, waɗanda zaku iya samu a Python Module Index.

Hakanan zaka iya shigo da dakunan karatu don shirin Python ɗinku, daga “gi.repository import Gtk” wannan layin yana shigo da ɗakin karatu na GTK cikin shirin Python, akwai sauran ɗakunan karatu da yawa kamar Gdk, WebKit.. da sauransu.

Ƙirƙirar Aikace-aikacen GUI na gaba

A yau, za mu ƙirƙiri shirye-shirye guda 2:

  1. Mai binciken gidan yanar gizo mai sauƙi; wanda zai yi amfani da ɗakin karatu na WebKit.
  2. Mai rikodin tebur ta amfani da umarnin 'avconv'; wanda zai yi amfani da tsarin ‘os’ daga Python.

Ba zan yi bayanin yadda ake ja da sauke widget din cikin mai tsara Glade daga yanzu ba, kawai zan gaya muku sunan widget din da kuke buƙatar ƙirƙira, bugu da ƙari zan ba ku >.gladefayil don kowane shiri, da kuma fayil ɗin Python tabbas.

Domin ƙirƙirar burauzar gidan yanar gizon, dole ne mu yi amfani da injin \WebKit, wanda shine ingin ma'anar buɗewa ga gidan yanar gizon, daidai yake da wanda ake amfani dashi a >Chrome/Chromium, don ƙarin bayani game da shi kuna iya komawa gidan yanar gizon Webkit.org na hukuma.

Da farko, za mu ƙirƙiri GUI, buɗe Glade mai ƙira kuma mu ƙara widgets masu zuwa. Don ƙarin bayani kan yadda ake ƙirƙirar widgets, bi Sashe na 1 da Sashe na 2 na wannan jerin (hanyoyin da aka bayar a sama).

  1. Ƙirƙiri widget din 'window1'.
  2. Ƙirƙiri widget din 'akwatin1' da 'box2'.
  3. Ƙirƙiri 'button1' da 'button2' widget din.
  4. Ƙirƙiri widget din 'shiga1'.
  5. Ƙirƙiri widget din 'scrolledwindow1'.

Bayan ƙirƙirar widgets, za ku sami mai biyowa dubawa.

Babu wani sabon abu, sai widget din \Taga mai gungurawa; wannan widget din yana da mahimmanci domin a ba da damar dasa injin WebKit a ciki, ta amfani da \ > Window mai gungurawa” widget din kuma zaku iya gungurawa a kwance da kuma a tsaye yayin da kuke zazzage gidajen yanar gizo.

Yanzu za ku ƙara \Backbutton_clicked mai kula zuwa maballin Baya \latsa siginar, \button_latsa b>” mai kula da maballin Refresh \latsa sigina da kuma \An danna maballin shiga .

Cikakken .glade fayil don dubawa yana nan.

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

Yanzu kwafi lambar da ke sama sannan ka liƙa a cikin fayil ɗin \ui.glade a cikin babban fayil ɗin ku. Yanzu ƙirƙirar sabon fayil mai suna \mywebbrowser.py sannan ku shigar da shi. lambar da ke cikinta, duk bayanin yana cikin sharhi.

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

Ajiye fayil ɗin, kuma kunna shi.

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

Kuma wannan shine abin da za ku samu.

Kuna iya koma ga WebKitGtk takaddun hukuma don gano ƙarin zaɓuɓɓuka.

A cikin wannan sashe, za mu koyi yadda ake tafiyar da umarnin tsarin gida ko rubutun harsashi daga fayil ɗin Python ta amfani da tsarin ''os', wanda zai taimaka mana wajen ƙirƙirar rikodin allo mai sauƙi don tebur ta amfani da 'avconv' umarni.

Bude mai zanen Glade, kuma ƙirƙirar widgets masu zuwa:

  1. Ƙirƙiri widget din 'window1'.
  2. Ƙirƙiri widget din 'akwatin1'.
  3. Ƙirƙiri 'button1', 'button2' da 'button3' widgets.
  4. Ƙirƙiri widget din 'shiga1'.

Bayan ƙirƙirar sama ce widgets, za ka samu a kasa dubawa.

Anan ga cikakken ui.glade fayil.

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

Kamar yadda aka saba, kwafi lambar da ke sama a liƙa a cikin fayil ɗin \ui.glade a cikin kundin adireshin gidanku, ƙirƙirar sabon fayil \myrecorder.py sannan shigar da shi. lambar da ke cikinsa (Kowane sabon layi yana bayyana a cikin sharhi).

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

Yanzu gudanar da fayil ɗin ta amfani da umarni masu zuwa a cikin tashar.

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

Kuma kun sami rikodin tebur ɗinku na farko.

Kuna iya samun ƙarin bayani game da tsarin ''os' a Laburaren Python OS.

Kuma shi ke nan, ƙirƙirar aikace-aikacen don tebur na Linux ba shi da wahala ta amfani da PyGObject, kawai dole ne ku ƙirƙiri GUI, shigo da wasu kayayyaki kuma ku haɗa fayil ɗin Python tare da GUI, babu ƙari, ba komai. Akwai koyawa masu amfani da yawa game da yin wannan a cikin gidan yanar gizon PyGObject:

Shin kun gwada ƙirƙirar aikace-aikace ta amfani da PyGObject? Me kuke tunani game da yin haka? Wadanne aikace-aikace kuka kirkira a baya?