Home  Contents

Widgets in PyGTK

الأدوات (الويدجات) هى وحدة بناء الواجهة، وتقدم لنا PyGTK الكثير من الأدوات المختلفة كالأزرار وصناديق الإختبار والمنزلقات والقوائم.. الخ ،كل مايحتاجه المبرمج فى عمله. سنتعرض لبعض الأدوات المفيدة فى هذه الجزئية



Label

يستخدم ال label لعرض نص ساكن -ملصق عنوان-

label.py
 
#!/usr/bin/python

# label.py

import gtk

lyrics = """Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
You say why did you do it with him today?
and sniff me out like I was Tanqueray

cause you're my fella, my guy
hand me your stella and fly
by the time I'm out the door
you tear men down like Roger Moore

I cheated myself
like I knew I would
I told ya, I was trouble
you know that I'm no good"""


class PyApp(gtk.Window): 
    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_border_width(8)
        self.connect("destroy", gtk.main_quit)
        self.set_title("You know I'm no Good")
        
        label = gtk.Label(lyrics)
        self.add(label)
        self.show_all()


PyApp()
gtk.main()

فى المثال نعرض كلمات اغنية على نص ساكن

lyrics = """Meet you downstairs in the bar and heard
your rolled up sleeves and your skull t-shirt
..."""

هذه هى الكلمات

 self.set_border_width(8)

الlabel محاط ببعض المساحة

 label = gtk.Label(lyrics)
 self.add(label)

انشاءه وإضافته


Label Widget

Figure: Label Widget



CheckButton

صندوق الإختيار CheckButton يستخدم لعرض حالتين on, off (منشط او غير منشط) ويستخدم من اجل الإختيارات المنطقية

checkbutton.py
 
#!/usr/bin/python

# checkbutton.py

import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()
        self.set_title("Check Button")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(250, 200)

        fixed = gtk.Fixed()
        button = gtk.CheckButton("Show title")
        button.set_active(True)
        button.unset_flags(gtk.CAN_FOCUS)
        button.connect("clicked", self.on_clicked)

        fixed.put(button, 50, 50)

        self.connect("destroy", gtk.main_quit)
        self.add(fixed)
        self.show_all()

    def on_clicked(self, widget):
        if widget.get_active():
            self.set_title("Check Button")
        else:
           self.set_title("")
        
PyApp()
gtk.main()

سنعرض العنوان على شريط العنوان معتمدا على حالة صندوق الإختيار

 button = gtk.CheckButton("Show title")

انشاء زر الإختيار

 button.set_active(True)

ننشطه لنجعل العنوان متاحا فى البداية

 if widget.get_active():
     self.set_title("Check Button")
 else:
     self.set_title("")

اذا كان منشط نعرض العنوان وإلا فلا


CheckButton

Figure: CheckButton



ComboBox

ال ComboBox يسمح بإلإختيار من قائمة ما

combobox.py

#!/usr/bin/python

# combobox.py

import gtk

class PyApp(gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_title("ComboBox")
        self.set_default_size(250, 200)
        self.set_position(gtk.WIN_POS_CENTER)

        cb = gtk.combo_box_new_text()
        cb.connect("changed", self.on_changed)

        cb.append_text('Ubuntu')
        cb.append_text('Mandriva')
        cb.append_text('Redhat')
        cb.append_text('Gentoo')
        cb.append_text('Mint')
        
        fixed = gtk.Fixed()
        fixed.put(cb, 50, 30)
        self.label = gtk.Label("-")
        fixed.put(self.label, 50, 140)
        self.add(fixed)

        self.connect("destroy", gtk.main_quit)
        self.show_all()


    def on_changed(self, widget):
        self.label.set_label(widget.get_active_text()) 

PyApp()
gtk.main()

الأمثلة تعرض ComboBox (تشمل 6 عناصر وهى اسماء لتوزيعات لينكس) وlabel ليستخدم لعرض التوزيعة المختارة

 cb = gtk.combo_box_new_text()

انشاء الComboBox عن طريق الدالة combo_box_new_text اللتى تنششئ لنا كائن Combobox عناصره مكونة من نصوص

 cb.append_text('Ubuntu')
 cb.append_text('Mandriva')
 cb.append_text('Redhat')
 cb.append_text('Gentoo')
 cb.append_text('Mint')

إضافة العناصر

 self.label.set_label(widget.get_active_text()) 

فى الطريقة on_changed نحصل على النص المختار ونعرضه على ال label


ComboBox

Figure: ComboBox



Image

فى المثال، سنقدم ال Image ويدجت المستخدم لعرض الصور

image.py

#!/usr/bin/python

# image.py

import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()
        
        self.set_title("Red Rock")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_border_width(2)

        image = gtk.Image()
        image.set_from_file("redrock.png")

        self.connect("destroy", gtk.main_quit)
        self.add(image)
        self.show_all()

PyApp()
gtk.main()

نعرض قلعة الصخرة الحمراء على النافذة

 image = gtk.Image()

انشاء كائن الصورة

 image.set_from_file("redrock.png")

نحدد الصورة من ملف عن طريق الطريقة set_from_file


Image

Figure: Image



تعرضنا لبعض الويدجات الأساسية فى PyGTK


Home ‡ Contents ‡ Top of Page