Oħloq l-Applikazzjonijiet tal-Web Browser u tar-Reġistratur tad-Desktop tiegħek stess billi tuża PyGobject - Parti 3


Din hija t-3rd parti tas-serje dwar il-ħolqien ta' applikazzjonijiet GUI taħt id-desktop tal-Linux bl-użu ta' PyGObject. Illum ser nitkellmu dwar l-użu ta' xi moduli u libreriji Python avvanzati fil-programmi tagħna bħal 'os', 'WebKit', 'talbiet' u oħrajn, minbarra xi informazzjoni utli oħra għall-ipprogrammar.

Trid tgħaddi minn dawn il-partijiet preċedenti kollha tas-serje minn hawn, biex tkompli struzzjonijiet ulterjuri dwar il-ħolqien ta' aktar applikazzjonijiet minn qabel:

  1. Oħloq Applikazzjonijiet GUI taħt il-Linux Desktop billi tuża PyGObject – Parti 1
  2. Ħolqien ta' Applikazzjonijiet Advance PyGobject fuq Linux – Parti 2

Moduli u libreriji f'Python huma utli ħafna, minflok ma tikteb ħafna sottoprogrammi biex tagħmel xi xogħlijiet ikkumplikati li jieħdu ħafna ħin u xogħol, tista' timportahom biss! Iva, jimporta biss il-moduli u l-libreriji li għandek bżonn għall-programm tiegħek u tkun tista 'tiffranka ħafna ħin u sforz biex tlesti l-programm tiegħek.

Hemm ħafna moduli famużi għal Python, li tista 'ssib fl-Indiċi tal-Modulu Python.

Tista 'timporta libreriji wkoll għall-programm Python tiegħek, minn gi.repository import Gtk din il-linja timporta l-librerija GTK fil-programm Python, hemm ħafna libreriji oħra bħal Gdk, WebKit.. eċċ.

Ħolqien ta 'Applikazzjonijiet GUI bil-quddiem

Illum, ser noħolqu 2 programmi:

  1. Web browser sempliċi; li se juża l-librerija tal-WebKit.
  2. Reġistratur tad-desktop li juża l-kmand ‘avconv’; li se juża l-modulu ‘os’ minn Python.

Mhux se nispjega kif drag & drop widgets fid-disinjatur Glade minn issa 'l quddiem, jien se ngħidlek biss l-isem tal-widgets li għandek bżonn toħloq, barra minn hekk nagħtik il-.glade fajl għal kull programm, u l-fajl Python żgur.

Sabiex noħolqu web browser, ikollna nużaw il-magna \WebKit”, li hija magna ta' rendering open-source għall-web, hija l-istess waħda li tintuża f'Kromju/Kromju, għal aktar informazzjoni dwaru tista' tirreferi għall-websajt uffiċjali Webkit.org.

L-ewwel, ser ikollna noħolqu l-GUI, niftħu d-disinjatur Glade u żid il-widgets li ġejjin. Għal aktar informazzjoni dwar kif toħloq widgets, segwi l-Parti 1 u l-Parti 2 ta’ din is-serje (links mogħtija hawn fuq).

  1. Oħloq widget ‘window1’.
  2. Oħloq widget ‘box1’ u ‘box2’.
  3. Oħloq widget ‘button1’ u ‘button2’.
  4. Oħloq widget ‘entry1’.
  5. Oħloq widget ‘scrolledwindow1’.

Wara li toħloq widgets, ikollok l-interface li ġejja.

M'hemm xejn ġdid, ħlief il-widget \Scrolled Window”; dan il-widget huwa importanti sabiex jippermetti li l-magna WebKit tiġi impjantata ġewwa fih, bl-użu tal-\Scrolled Window” widget inti tkun kapaċi wkoll tiskrollja orizzontalment u vertikalment waqt li tfittex il-websajts.

Issa jkollok iżżid il-handler ta' \backbutton_clicked” mas-sinjal Lura buttuna \ikklikkja”, \refreshbutton_clicked b>” handler għall-buttuna Aġġorna \sinjal ikklikkjat” u \enterkey_clicked” handler għas-sinjal \attivat” għad-dħul .

Il-fajl sħiħ .glade għall-interface jinsab hawn.

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

Issa kkopja l-kodiċi ta’ hawn fuq u waħħalha fil-fajl \ui.glade” fil-folder tad-dar tiegħek. Issa oħloq fajl ġdid imsejjaħ \mywebbrowser.py” u daħħal il-kodiċi li ġej ġewwa fih, l-ispjegazzjoni kollha tinsab fil-kummenti.

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

Issejvja l-fajl, u mexxih.

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

U dan huwa dak li se tikseb.

Tista' tirreferi għad-dokumentazzjoni uffiċjali WebKitGtk sabiex tiskopri aktar għażliet.

F'din it-taqsima, ser nitgħallmu kif inħaddmu kmandi tas-sistema lokali jew skripts tal-qoxra mill-fajl Python billi tuża l-modulu 'os', li se jgħinna noħolqu screen recorder sempliċi għad-desktop billi tuża l- kmand 'avconv'.

Iftaħ id-disinjatur Glade, u oħloq il-widgets li ġejjin:

  1. Oħloq widget ‘window1’.
  2. Oħloq widget ‘box1’.
  3. Oħloq widgets ‘button1’, ‘button2’ u ‘button3’.
  4. Oħloq widget ‘entry1’.

Wara li toħloq hawn fuq imsemmi widgets, inti se tikseb hawn taħt interface.

Hawn hu l-fajl komplut 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>

Bħas-soltu, ikkupja l-kodiċi ta’ hawn fuq u waħħalha fil-fajl \ui.glade” fid-direttorju tad-dar tiegħek, oħloq fajl ġdid \myrecorder.py u daħħal il-kodiċi li ġej ġewwa fih (Kull linja ġdida hija spjegata fil-kummenti).

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

Issa mexxi l-fajl billi tapplika l-kmandi li ġejjin fit-terminal.

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

U inti ltqajna l-ewwel desktop recorder tiegħek.

Tista' ssib aktar informazzjoni dwar il-modulu 'os' fil-Librerija OS Python.

U dan hu, il-ħolqien ta 'applikazzjonijiet għad-desktop tal-Linux mhuwiex diffiċli bl-użu ta' PyGObject, għandek biss toħloq il-GUI, timporta xi moduli u torbot il-fajl Python mal-GUI, xejn aktar, xejn inqas. Hemm ħafna tutorials utli dwar kif tagħmel dan fil-websajt ta' PyGObject:

Ippruvajt toħloq applikazzjonijiet billi tuża PyGObject? X'taħseb dwar li tagħmel hekk? Liema applikazzjonijiet żviluppajt qabel?