金曜日, 7月 03, 2009

Creating a GUI using PyGTK and Glade

Creating a GUI using PyGTK and Glade


The First Step:
#!/usr/bin/env python

import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)


The Full Sample(Glade Object File:Libglade)
#!/usr/bin/env python

import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)

class HellowWorldGTK:
    """This is an Hello World GTK application"""

    def __init__(self):
        
        #Set the Glade file
        self.gladefile = "pyhelloworld.glade"  
        self.wTree = gtk.glade.XML(self.gladefile)
        
        #Create our dictionay and connect it
        dic = { "on_btnHelloWorld_clicked" : self.btnHelloWorld_clicked,
            "on_MainWindow_destroy" : gtk.main_quit }
        self.wTree.signal_autoconnect(dic)

    def btnHelloWorld_clicked(self, widget):
        print "Hello World!"


if __name__ == "__main__":
    hwg = HellowWorldGTK()
    gtk.main()


The Full Sample(Glade Object File:GtkBuilder)
#!/usr/bin/env python

import pygtk
import gtk
pygtk.require("2.0")

class GUI(object):
    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file("GUI.xml")
        builder.connect_signals(self)
        self.window1 = builder.get_object("window1")
        self.window1.show()

    def on_window1_destroy(self,widget,data=None):
        gtk.main_quit()

    def on_button1_clicked(self,widget,data=None):
        gtk.main_quit()  

if __name__ == "__main__":
    app = GUI()
    gtk.main()


More Simple Sample(test003.py)
#!/usr/bin/env python

import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    #Libglade
    #import gtk.glade
except:
    sys.exit(1)

class TestMain:
    """This is an Hello World GTK application"""

    def __init__(self):
        
        #Set the Glade file
        self.gladefile = "test003.glade"
        self.glade = gtk.Builder()
        self.wTree = self.glade.add_from_file(self.gladefile)

        #Create our dictionay and connect it
        #dic = { "on_btnHelloWorld_clicked" : self.btnHelloWorld_clicked,
        #    "on_windowMaindow_destroy" : gtk.main_quit }
        #1.Libglade Auto Connect
        #self.wTree.signal_autoconnect(dic)
        #2.GktBuilder Auto Connect
        #glader.connect_signals(dic)

        #Main Window
        self.windowMain = self.glade.get_object("windowMain")
        self.windowMain.connect("destroy", self.on_windowMain_destroy)
        self.windowMain.show()

        #GktImage
        self.imageMain = self.glade.get_object("imageMain")
        self.imageMain.set_from_file("airplane.jpg")
        #MenuItem Activate
        self.menuitemQuit = self.glade.get_object("imagemenuitemQuit")
        self.menuitemQuit.connect("activate", self.on_windowMain_destroy)

    def on_windowMain_destroy(self, widget, data=None):
        gtk.main_quit()

    def btnHelloWorld_clicked(self, widget, data=None):
        print "Hello World!"

if __name__ == "__main__":
    hwg = TestMain()
    gtk.main()


And the test003.glade file
<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="windowMain">
    <child>
      <object class="GtkVBox" id="vboxMain">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubarMain">
            <property name="visible">True</property>
            <child>
              <object class="GtkMenuItem" id="menuitemFile">
                <property name="visible">True</property>
                <property name="label" translatable="yes">&#x30D5;&#x30A1;&#x30A4;&#x30EB;(_F)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menuFile">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitemNew">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitemOpen">
                        <property name="label">gtk-open</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitemSave">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitemSaveAs">
                        <property name="label">gtk-save-as</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
                        <property name="visible">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitemQuit">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem2">
                <property name="visible">True</property>
                <property name="label" translatable="yes">&#x7DE8;&#x96C6;(_E)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem6">
                        <property name="label">gtk-cut</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem7">
                        <property name="label">gtk-copy</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem8">
                        <property name="label">gtk-paste</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem9">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem3">
                <property name="visible">True</property>
                <property name="label" translatable="yes">&#x8868;&#x793A;(_V)</property>
                <property name="use_underline">True</property>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem4">
                <property name="visible">True</property>
                <property name="label" translatable="yes">&#x30D8;&#x30EB;&#x30D7;(_H)</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem10">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkImage" id="imageMain">
            <property name="visible">True</property>
            <property name="stock">gtk-missing-image</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkStatusbar" id="statusbarMain">
            <property name="visible">True</property>
            <property name="spacing">2</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

0 件のコメント: