3 קלח - PyGobject תועצמאב טילקמ ןחלושו ךלשמ טנרטניא ןפדפד ימושיי רוצ


.תונכתל רחא ישומיש עדימ דצל ,םירחא -ו ' תושקב os ', ' WebKit ', '' ומכ ונלש תוינכותב םימדקתמ Python תוירפסו םילודומ המכב שומיש לע רבדנ םויה .PyGObject תועצמאב סקוניל

:םיפסונ םימידקמ םימושיי תריציל תופסונ תוארוהב ךישמהל ידכ ,ןאכמ הרדסה לש םימדוקה םיקלחה לכ לע רובעל ךילע

    .ךלש תינכותה תמלשהל ץמאמו ןמז הברה ךוסחל לכותו ךלש תינכותל ךירצ התאש תוירפסהו םילודומה תא אביי טושפ ,ןכ !ןתוא אבייל טושפ לוכי התא ,הדובעו ןמז הברה חקייש תוכבוסמ תודובע עצבל ידכ תובר הנשמ ת

    .Python Module סקדניאב אוצמל ולכות םתוא ,Python רובע םימסרופמ םילודומ הברה םנשי

    .' וכו .. Gdk, WebKit ומכ תורחא תובר תוירפס ןנשי ,Python תינכותל GTK תיירפס תא תאביימ וז הרוש " gi.repository import Gtk " ךותמ ,ךלש ןותייפה תינכות רובע םג תוירפס אבייל ךתורשפאב

    םימדקתמ GUI ימושיי תריצי

    :תוינכות יתש רוצינ םויה

      .תואדווב Python -ה ץבוקו ,תינכות לכל ץבוק .glade ה תא ךל ןתא ףסונב ,רוציל ךירצ התאש םיט'גדיווה םש תא ךל דיגא טושפ Glade בצעמב םיט'גדיוו ררחשו רורגל דציכ ריבסא אל

      .Webkit.org ימשרה רתאל תונפל ולכות ,ויתודוא ףסונ עדימ תלבקל , Chrome / Chromium -ב שומישל ההז הז ,טנרטניאל חותפ דוק דוביע עונמ אוהש ," WebKit "\עונמב שמתשהל ךרטצ

      .(ליעל םיעיפומה םירושיק) וז הרדס לש 2 קלח ו 1 קלח רחא בוקע ,םיט'גדיוו תריצי ןפוא לע ףסונ עדימל .םיאבה םיט'גדיווה תא ףיסוהלו b> Glade> בצעמה תא חותפל ,שמתשמה קשממ תא רוצ

        .אבה קשממה תא לבקת ,םיט'גדיוו תריצי רחאל

        .םירתאב שלוג התאש ןמזב תיכנאו תיקפוא לולגל םג לכות" לולג ןולח ןומושיי < b>"\תועצמאב WebKit ה עונמ תא וכותב ליתשהל רשפאל תנמ לע בושח הז ןומושיי ;" לגלוגמ ןולח "\ן

        . הסינכה רובע" לעפומ "\ תואל" enterkey_clicked "\ לפטמו" תוא לע ץחל "\ ןונערה רותפכב לפטמ" <ב /> b>",\" refreshbutton_clicked /> וצחל Back \"

        .ןאכ אצמנ קשממל .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 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>
        

        .תובוגתב אצמנ רבסהה לכ ,וכותב אבה דוקה ןזהו " mywebbrowser.py "\םשב שדח ץבוק רוצ תעכ .ךלש תיבה תייקיתב " ui.glade "\ץבוקב ותוא קבדהו ליעלש דוקה תא קתעה תעכ

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

        .ותוא לעפהו ץבוקה תא רומש

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

        .ולבקתש המ הזו

        .תופסונ תויורשפא תולגל תנמ לע WebKitGtk ימשרה דועיתב ןייעל לכות

        .' avconv ' הדוקפה תועצמאב הדובעה ןחלושל טושפ ךסמ טילקמ רוציל ונל רוזעיש ,' os ' לודומה תועצמאב ןותייפ ץבוקמ תפטעמ לש םיטפירקס וא תוימוקמ תכרעמ תודוקפ ליעפהל דציכ דמלנ הז קלח

        :םיאבה םיט'גדיווה תא רוצו Glade בצעמ תא חתפ

          .קשממל תחתמ עיגת ,ל"נה םינומושייה תריצי רחאל

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

          .(תובוגתב תרבסומ השדח הרוש לכ) וכותב אבה דוקה ןזהו שדח " myrecorder.py "\ץבוק רוצ ,ךלש תיבה תיירפסב " ui.glade "\ץבוקב ותוא קבדהו ליעלש דוקה תא קתעה ,ליגרכ

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

          .ףוסמב תואבה תודוקפה תלחה ידי לע ץבוקה תא לעפה תעכ

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

          .ךלש ןושארה הדובעה ןחלוש טילקמ תא תלביקו

          .Python לש הלעפהה תכרעמ תיירפסב ' os ' לודומה לע ףסונ עדימ אוצמל לכות

          :PyGObject רתאב תאז תושעל אשונב םיישומיש םיכירדמ הברה שי .תוחפ אל ,רתוי אל ,GUI -ל Python -ה ץבוק תא רשקלו םילודומ המכ אבייל ,GUI -ה תא רוציל ךירצ קר התא , PyGObject תועצמאב השק הנ

          ?רבעב תחתיפ םימושיי וליא ?ךכ לע בשוח התא המ ?PyGObject תועצמאב םימושיי רוציל תיסינ


כל הזכויות שמורות. © Linux-Console.net • 2019-2024