PyGobject ulanyp, öz web brauzeriňizi we iş stoly ýazgy programmalaryny dörediň - 3-nji bölüm


Bu, PyGObject ulanyp Linux iş stolunyň aşagynda GUI programmalaryny döretmek baradaky seriýanyň 3-nji bölümi. Bu gün programmalarymyzda ' os ', ' WebKit ', ' haýyşlar ' ýaly ösen Python modullaryny we kitaphanalaryny ulanmak barada gürleşeris. beýlekiler, programmirlemek üçin käbir peýdaly maglumatlardan başga-da.

Has öňdebaryjy programmalary döretmek boýunça goşmaça görkezmeleri dowam etdirmek üçin seriýanyň öňki bölümleriniň hemmesini şu ýerden öwrenmeli:

  1. PyGObject ulanyp Linux iş stolunyň aşagynda GUI programmalaryny dörediň - 1-nji bölüm
  2. Linux-da Advance PyGobject programmalaryny döretmek - 2-nji bölüm

Python-daky modullar we kitaphanalar gaty peýdaly, köp wagt we işlemeli käbir çylşyrymly işleri ýerine ýetirmek üçin köp kiçi programma ýazmagyň ýerine, diňe import edip bilersiňiz! Hawa, diňe programmaňyza zerur modullary we kitaphanalary import ediň we programmaňyzy tamamlamak üçin köp wagt we güýji tygşytlap bilersiňiz.

Python üçin belli modullar bar, olary Python Module Index-de tapyp bilersiňiz.

Python programmaňyz üçin kitaphanalary hem import edip bilersiňiz, “ gi.repository import Gtk ” -den bu setir GTK kitaphanasyny Python programmasyna import edýär, Gdk, WebKit .. we başga-da köp kitaphanalar bar.

Advance GUI programmalaryny döretmek

Bu gün 2 sany programma dörederis:

  1. simpleönekeý web brauzeri; WebKit kitaphanasyny ulanar.
  2. avconv buýrugyny ulanyp, iş stoly ýazgysy; Python-dan “os” modulyny ulanar.

Widjetleri Glade dizaýnerinde nädip süýräp we taşlamalydygyny düşündirip bilmerin, diňe size döretmeli widjetleriň adyny aýdaryn, goşmaça size her programma üçin .glade faýly we Python faýly.

Web brauzeri döretmek üçin web üçin açyk çeşme görkeziji hereketlendiriji bolan\ WebKit hereketlendirijisini ulanmaly bolarys, Chrome / Hrom , bu hakda has giňişleýin maglumat üçin resmi Webkit.org web sahypasyna ýüz tutup bilersiňiz.

Ilki bilen GUI döretmeli, Glade dizaýnerini açmaly we aşakdaky gurallary goşmaly. Widjetleri nädip döretmelidigi barada has giňişleýin maglumat üçin şu seriýanyň 1-nji bölümi we 2-nji bölüm yzarlaň (ýokarda görkezilen baglanyşyklar).

  1. Window1 widjetini dörediň.
  2. box1 we box2 widjetini dörediň.
  3. düwme1 we düwme2 widjetini dörediň.
  4. enter1 widjetini dörediň.
  5. scrollwindow1 widjetini dörediň.

Widjetler döredilenden soň, aşakdaky interfeýsi alarsyňyz.

\ Aýlanan penjire widjetinden başga hiç zat ýok; bu widjet\ WebKit motorynyň içine ornaşdyrylmagy üçin möhümdir. > Aýlanan penjire ”widjeti, web sahypalaryna göz aýlanyňyzda keseligine we dikligine aýlanyp bilersiňiz.

Indi Yzyna düwmesine\ basyldy signalyna,\ täzelenme düwmesi_/basylan arka düwmesi_köplenen işleýjisini goşmaly bolarsyňyz. b> Täzeleme düwmesine işleýji\ basylan signal we giriş üçin\ işjeňleşdirilen signalyna\ enterkey_clicked işleýjisi .

Interfeýs üçin doly .glade faýly şu ýerde.

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

Indi ýokardaky kody göçüriň we öý bukjasyndaky\ ui.glade faýlyna goýuň. Indi\ mywebbrowser.py atly täze faýl dörediň we giriň içindäki aşakdaky kod, ähli düşündiriş teswirlerde.

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

Faýly ýazdyryň we işlediň.

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

Ine, şuny alarsyň.

Has köp mümkinçilik tapmak üçin WebKitGtk resmi resminamalaryna ýüz tutup bilersiňiz.

Bu bölümde, os modulyny ulanyp, Python faýlyndan ýerli ulgam buýruklaryny ýa-da gabyk skriptlerini nädip işletmelidigini öwreneris, bu bolsa iş stoly üçin ýönekeý ekran ýazgysyny döretmäge kömek eder. ' avconv ' buýrugy.

Glade dizaýnerini açyň we aşakdaky gurallary dörediň:

  1. Window1 widjetini dörediň.
  2. box1 widjetini dörediň.
  3. düwme1, düwme2 we düwme3 gurallaryny dörediň.
  4. enter1 widjetini dörediň.

Aboveokarda aýdylan gurallary döredeniňizden soň, interfeýsiň aşagyna girersiňiz.

Ine, doly ui.glade faýly.

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

Hemişe bolşy ýaly ýokardaky kody göçüriň we öý bukjasyndaky\ ui.glade faýlyna goýuň, täze\ myrecorder.py faýly dörediň we giriň içindäki aşakdaky kod (Teswirlerde her täze setir düşündirilýär).

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

Indi terminalda aşakdaky buýruklary ulanyp faýly işlediň.

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

Ilkinji iş stoly ýazgysyny aldyňyz.

Python OS kitaphanasynda os moduly barada has giňişleýin maglumat tapyp bilersiňiz.

Linux iş stoly üçin programmalar döretmek PyGObject ulanmak kyn däl, diňe GUI döretmeli, käbir modullary import etmeli we Python faýlyny GUI bilen baglanyşdyrmaly, başga hiç zat, az zat ýok. PyGObject web sahypasynda muny etmek barada köp peýdaly gollanmalar bar:

PyGObject ulanyp programmalar döretmäge synanyşdyňyzmy? Şeýle etmek hakda näme pikir edýärsiňiz? Öň haýsy programmalary düzdüňiz?