Izveidojiet savu tīmekļa pārlūku un darbvirsmas ierakstītāju, izmantojot PyGobject - 3. daļa


Šī ir sērijas 3. daļa par GUI lietojumprogrammu izveidi Linux darbvirsmā, izmantojot PyGObject. Šodien mēs runāsim par dažu uzlabotu Python moduļu un bibliotēku izmantošanu mūsu programmās, piemēram, ' os ', ' WebKit ', ' pieprasījumi ' un citi, bez citas noderīgas informācijas programmēšanai.

Šeit ir jāiziet visas šīs iepriekšējās sērijas daļas, lai turpinātu sniegt papildu norādījumus par iepriekšēju lietojumprogrammu izveidi:

  1. Veidojiet GUI lietojumprogrammas Linux darbvirsmā, izmantojot PyGObject - 1. daļa
  2. Advance PyGobject lietojumprogrammu izveide Linux - 2. daļa

Moduļi un bibliotēkas Python ir ļoti noderīgi, tā vietā, lai rakstītu daudzas apakšprogrammas, lai veiktu sarežģītus darbus, kas prasīs daudz laika un darba, jūs varat tos vienkārši importēt! Jā, vienkārši importējiet nepieciešamos moduļus un bibliotēkas savā programmā, un jūs varēsiet ietaupīt daudz laika un pūļu, lai pabeigtu programmu.

Python ir daudz slavenu moduļu, kurus varat atrast vietnē Python Module Index.

Jūs varat importēt bibliotēkas arī savai Python programmai, no “ gi.repository import Gtk ” šī rinda importē GTK bibliotēku Python programmā, ir daudzas citas bibliotēkas, piemēram, Gdk, WebKit .. utt.

Iepriekšēju GUI lietojumprogrammu izveide

Šodien mēs izveidosim 2 programmas:

  1. vienkārša tīmekļa pārlūkprogramma; kas izmantos WebKit bibliotēku.
  2. darbvirsmas ierakstītājs, izmantojot komandu ‘avconv’; kas izmantos Python moduli ‘os’.

Turpmāk es nepaskaidrošu, kā vilkt un nomest logrīkus dizainerī Glade , es jums pateiksšu tikai to logrīku nosaukumus, kas jums jāizveido, turklāt es jums došu .glade fails katrai programmai un noteikti Python fails.

Lai izveidotu tīmekļa pārlūkprogrammu, mums būs jāizmanto dzinējs WebKit , kas ir atvērtā koda renderēšanas dzinējs tīmeklim, tas ir tas pats, kas tiek izmantots Chrome / Chromium , lai iegūtu papildinformāciju par to, apmeklējiet oficiālo Webkit.org vietni.

Pirmkārt, mums būs jāizveido GUI, jāatver Glade dizainers un jāpievieno šādi logrīki. Lai iegūtu papildinformāciju par logrīku izveidi, skatiet šīs sērijas 1. daļu un 2. daļu (iepriekš norādītās saites).

  1. Izveidojiet logrīku “window1”.
  2. Izveidojiet logrīku “box1” un “box2”.
  3. Izveidojiet logrīku “button1” un “button2”.
  4. Izveidojiet logrīku ‘entry1’.
  5. Izveidojiet logrīku “scrolledwindow1”.

Pēc logrīku izveides jūs saņemsiet šādu saskarni.

Nav nekā jauna, izņemot logrīku Ritinātais logs ; Šis logrīks ir svarīgs, lai ļautu tajā ievietot dzinēju WebKit , izmantojot logrīku Ritinātais logs , jūs varēsiet arī ritināt horizontāli un vertikāli, kamēr jūs pārlūkot vietnes.

Tagad jums būs jāpievieno “ backbutton_clicked ” apdarinātājs pogai Back clicked ” signālam “ refreshbutton_clicked ”. apdarinātāju pogai Atsvaidzināt “ noklikšķināts signāls ” un “ enterkey_clicked ” - ar ierakstu “aktivizēts ” signāls.

Pilns saskarnes .glade fails ir šeit.

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

Tagad nokopējiet iepriekš minēto kodu un ielīmējiet to savas mājas mapes failā “ ui.glade ”. Tagad izveidojiet jaunu failu ar nosaukumu “ mywebbrowser.py ” un tajā ievadiet šādu kodu, viss skaidrojums ir komentāros.

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

Saglabājiet failu un palaidiet to.

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

Un to jūs iegūsiet.

Lai uzzinātu vairāk iespēju, varat izmantot WebKitGtk oficiālo dokumentāciju.

Šajā sadaļā mēs uzzināsim, kā palaist vietējās sistēmas komandas vai čaulas skriptus no Python faila, izmantojot moduli ' os ', kas palīdzēs mums izveidot vienkāršu darbvirsmas ekrāna ierakstītāju, izmantojot komanda avconv .

Atveriet Glade dizaineru un izveidojiet šādus logrīkus:

  1. Izveidojiet logrīku “window1”.
  2. Izveidojiet logrīku “box1”.
  3. Izveidojiet logrīkus ‘button1’, ‘button2’ un ‘button3’.
  4. Izveidojiet logrīku ‘entry1’.

Pēc iepriekš minēto logrīku izveides jūs saņemsiet zemāk saskarni.

Šeit ir pilns fails 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>

Kā parasti, nokopējiet iepriekš minēto kodu un ielīmējiet to mājas direktorija failā “ ui.glade ”, izveidojiet jaunu failu “ myrecorder.py ” un ievadiet šo kods tā iekšienē (komentāros ir paskaidrota katra jaunā rinda).

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

Tagad palaidiet failu, terminālā lietojot šādas komandas.

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

Un jūs ieguvāt savu pirmo darbvirsmas ierakstītāju.

Plašāku informāciju par moduli ‘ os ’ varat atrast Python OS bibliotēkā.

Un tas ir viss, lietojumprogrammu PyGObject izveidošana Linux darbvirsmai nav grūta, jums vienkārši jāizveido GUI, jāimportē daži moduļi un jāsaista Python fails ar GUI, nekas vairāk, ne mazāk. PyGObject vietnē ir daudz noderīgu apmācību.

Vai esat mēģinājis izveidot lietojumprogrammas, izmantojot PyGObject? Ko jūs domājat, to darot? Kādas lietojumprogrammas esat izstrādājis iepriekš?