Mepụta ngwa GUI ndị ọzọ site na iji ngwa PyGobject na Linux - Akụkụ 2


Anyị na-aga n'ihu usoro anyị banyere ịmepụta ngwa GUI n'okpuru desktọpụ Linux site na iji PyGObject, Nke a bụ akụkụ nke abụọ nke usoro ahụ ma taa anyị ga-ekwu maka ịmepụta ngwa ndị ọzọ na-arụ ọrụ site na iji ụfọdụ wijetị dị elu.

  1. Mepụta ngwa GUI n'okpuru Linux Iji PyGObject - Nkebi 1

N'isiokwu gara aga, anyị kwuru na e nwere ụzọ abụọ maka ịmepụta ngwa GUI site na iji PyGObject: the code-only-way na Glade ụzọ mmebe , ma site ugbu a gaa n'ihu, naanị anyị ga-akọwa Glade ụzọ mmebe ebe ọ dị mfe maka ọtụtụ ndị ọrụ, ị nwere ike ịmụta koodu naanị-ụzọ site n'onwe gị site na iji python-gtk3-tutorial.

Ịmepụta ngwa GUI Ọganihu na Linux

1. Ka anyị malite mmemme! Mepee Glade mmebe gị site na menu ngwa.

2. Pịa na bọtịnụ \Ohere dị n'akụkụ aka ekpe iji mepụta nke ọhụrụ.

3. Pịa na wijetị \Igbe wee hapụ ya na windo efu.

4. A ga-akpali gị itinye nọmba igbe ịchọrọ, mee ya 3.

Ị ga-ahụkwa na a na-emepụta igbe, igbe ndị ahụ dị anyị mkpa ka anyị wee nwee ike itinye ihe karịrị naanị 1 wijetị na windo.

5. Ugbu a pịa na wijetị igbe, ma gbanwee ụdị nghazi site na vetikal ka ọ bụrụ horizontal.

6. Iji mepụta mmemme dị mfe, tinye otu \Etinye ederede, \Combo Box Text na a \bọtịnụ ” wijetị maka igbe ọ bụla, ịkwesịrị ịnwe ihe dị ka nke a.

7. Ugbu a pịa wijetị \window1 site na akụkụ aka nri, ma gbanwee ọnọdụ ya gaa na \Center.

Pịgharịa gaa na ngalaba \ọdịdị .. Tinye aha maka mpio Mmemme m.

8. Ị nwekwara ike họrọ akara ngosi maka mpio ahụ site na ịpị igbe \Aha akara ngosi.

9. Ị nwekwara ike ịgbanwe ndabara ịdị elu & obosara maka ngwa .. Mgbe niile, ị ga-enwe ihe dị ka nke a.

N'ime mmemme ọ bụla, otu n'ime ihe kachasị mkpa bụ ịmepụta windo \Ihe, iji mee nke a, anyị ga-ebu ụzọ gbanwee bọtịnụ nkịtị anyị mepụtara na mbụ na bọtịnụ ngwaahịa, lee anya. na foto a.

10. Ugbu a, anyị ga-gbanwee ụfọdụ mgbaàmà iji na-agba ọsọ kpọmkwem omume mgbe ihe ọ bụla omume mere na anyị wijetị. Pịa na wijetị ntinye ederede, gbanwee gaa na taabụ \signals dị n'akụkụ aka nri, chọọ \arụ ọrụ wee gbanwee ya. Handler to \enter_button_clicked, akara \arụ ọrụ bụ akara ndabara nke ezigara mgbe igodo \Tinye kụrụ. ka ị na-elekwasị anya na wijetị ntinye ederede.

Anyị ga-agbakwunye onye njikwa ọzọ maka akara ngosi \pịa maka wijetị bọtịnụ anyị, pịa ya wee gbanwee akara \pịa gaa na \< b>bọtịnụ_a_pịa“.

11. Gaa na taabụ \Common wee kaa akara na \Has Focus dị ka ọ na-esote ( Iji nye ndabara elekwasị anya maka bọtịnụ banyere ihe kama ntinye) .

12. Ugbu a si n'akụkụ aka ekpe, mepụta windo ọhụrụ \Ihe gbasara dialog.

Ị ga-ahụkwa na e mebere windo \About Dialog.

Ka anyị gbanwee ya .. Jide n'aka na itinye ntọala ndị a maka ya site na akụkụ aka nri.

Mgbe ịmechara ntọala dị n'elu, ị ga-enweta ihe gbasara Window.

Na mpio dị n'elu, ị ga-ahụ ohere efu, mana ị nwere ike wepu ya site na ibelata ọnụ ọgụgụ igbe site na 3 ruo 2 ma ọ bụ tinye wijetị ọ bụla na ya ma ọ bụrụ na ịchọrọ.

13. Ugbu a, chekwaa faịlụ na folda ụlọ gị n'aha \ui.glade wee mepee editọ ederede wee tinye koodu na-esonụ n'ime ya.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from gi.repository import Gtk
class Handler:

    def button_is_clicked(self, button):
        ## The ".run()" method is used to launch the about window.
         ouraboutwindow.run()
        ## This is just a workaround to enable closing the about window.
         ouraboutwindow.hide()

    def enter_button_clicked(self, button):
        ## The ".get_text()" method is used to grab the text from the entry box. The "get_active_text()" method is used to get the selected item from the Combo Box Text widget, here, we merged both texts together".
         print ourentry.get_text() + ourcomboboxtext.get_active_text()

## Nothing new here.. We just imported the 'ui.glade' file.
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
builder.connect_signals(Handler())

ournewbutton = builder.get_object("button1")

window = builder.get_object("window1")

## Here we imported the Combo Box widget in order to add some change on it.
ourcomboboxtext = builder.get_object("comboboxtext1")

## Here we defined a list called 'default_text' which will contain all the possible items in the Combo Box Text widget.
default_text = [" World ", " Earth ", " All "]

## This is a for loop that adds every single item of the 'default_text' list to the Combo Box Text widget using the '.append_text()' method.
for x in default_text:
  ourcomboboxtext.append_text(x)

## The '.set.active(n)' method is used to set the default item in the Combo Box Text widget, while n = the index of that item.
ourcomboboxtext.set_active(0)
ourentry = builder.get_object("entry1")

## This line doesn't need an explanation :D
ourentry.set_max_length(15)

## Nor this do.
ourentry.set_placeholder_text("Enter A Text Here..")

## We just imported the about window here to the 'ouraboutwindow' global variable.
ouraboutwindow = builder.get_object("aboutdialog1")

## Give that developer a cookie !
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main

Chekwaa faịlụ n'ime ndekọ ụlọ gị n'okpuru aha ahụ \myprogram.py, ma nye ya ikike mebie ya.

$ chmod 755 myprogram.py
$ ./myprogram.py
This is what you will get, after running above script.

Tinye ederede n'igbe ntinye, pịa igodo \Tinye na ahụigodo, ị ga-ahụkwa na a na-ebipụta ahịrịokwu ahụ na shei.

Nke ahụ bụ maka ugbu a, ọ bụghị ngwa zuru oke, mana m chọrọ igosi gị otu esi ejikọta ihe ọnụ site na iji PyGObject, ị nwere ike ịlele usoro niile maka ihe niile GTK wijetị na gtkobject.

Naanị mụta usoro ndị ahụ, mepụta wijetị site na iji Glade, wee jikọọ akara ngosi site na iji faịlụ Python, Ọ bụ ya! Ọ naghị esiri enyi m ike.

Anyị ga-akọwa ihe ọhụrụ ndị ọzọ gbasara PyGObject n'akụkụ ndị ọzọ nke usoro a, ruo mgbe ahụ ka emelitere ma echefula ịnye anyị okwu gị gbasara akụkọ ahụ.