next up previous contents
Next: Label and Entry Up: PyGTK Previous: Buttons 與 Layout   Contents

Signals, Events and Functions

  1. Example1
    #!/usr/bin/python
    # coding: utf-8
    
    #import pygtk
    import gtk
    
    class myPy:
        def callback(self, widget, data):
            if data == "yes":
                print "yes"
            elif data == "no":
                print "no"
            else:
                print "yes|no"
    
        def __init__(self):
            window = gtk.Window(gtk.WINDOW_TOPLEVEL)
            "lambda 是匿名函式 x 為其參數"
            window.connect("destroy", lambda x: gtk.main_quit())
            window.set_title('視窗抬頭')
            window.set_keep_above(True)
    
            vbox1 = gtk.VBox(False, 2)
            hbox1 = gtk.HBox(homogeneous=True,spacing=5)
            hbox2 = gtk.HBox(homogeneous=True,spacing=5)
            hbox3 = gtk.HBox(homogeneous=True,spacing=5)
            yes = gtk.Button("yes")
            no = gtk.Button("no")
            close = gtk.Button(None,gtk.STOCK_CLOSE)
            close.connect("clicked", lambda x: gtk.main_quit())
            yes.connect("clicked", self.callback, 'yes')
            no.connect("clicked", self.callback, 'no')
            hbox1.add(yes)
            hbox1.add(no)
            hbox2.add(close)
            vbox1.pack_start(hbox1, True, True, 0)
            vbox1.pack_start(hbox2, True, True, 0)
            vbox1.pack_start(hbox3, True, True, 0)
            window.add(vbox1)
    
            window.show_all()
    
    myPy()
    gtk.main()
    
  2. Example2
    #!/usr/bin/python
    # coding: utf-8
    
    #import pygtk
    import gtk
    import subprocess 
    
    icondir="/usr/share/icons/gnome/16x16"
    yes_icon = icondir+"/stock/generic/stock_calc-accept.png"
    no_icon = icondir+"/stock/generic/stock_calc-cancel.png"
    
    def img_label_box(parent, img_file, label):
        box = gtk.HBox(False, 0)
        box.set_border_width(2)
        image = gtk.Image()
        image.set_from_file(img_file)
        label = gtk.Label(label)
        box.pack_start(image, False, False, 5)
        box.pack_start(label, False, False, 5)
        image.show()
        label.show()
        return box
    
    class myPy:
    	def callback(self, widget, data):
    		if data == "yes":
    			subprocess.Popen("gnome-terminal",shell=True)
    		elif data == "no":
    			print "no"
    		else:
    			print "yes|no"
    
    	def __init__(self):
    		window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    		"lambda 是匿名函式 x 為其參數"
    		window.connect("destroy", lambda x: gtk.main_quit())
    		window.set_title('視窗抬頭')
    		window.set_keep_above(True)
    
    		vbox1 = gtk.VBox(False, 2)
    		hbox1 = gtk.HBox(homogeneous=True,spacing=5)
    		hbox2 = gtk.HBox(homogeneous=True,spacing=5)
    		hbox3 = gtk.HBox(homogeneous=True,spacing=5)
    		yes = gtk.Button()
    		no = gtk.Button()
    		close = gtk.Button(None,gtk.STOCK_CLOSE)
    		close.set_use_stock(True)
    		close.connect("clicked", lambda x: gtk.main_quit())
    		yes.connect("clicked", self.callback, 'yes')
    		no.connect("clicked", self.callback, 'no')
    		box_yes = img_label_box(window, yes_icon, "YES")
    		box_no = img_label_box(window, no_icon, "NO")
    		yes.add(box_yes)
    		no.add(box_no)
    		hbox1.add(yes)
    		hbox1.add(no)
    		hbox2.add(close)
    		vbox1.pack_start(hbox1, True, True, 0)
    		vbox1.pack_start(hbox2, True, True, 0)
    		vbox1.pack_start(hbox3, True, True, 0)
    		window.add(vbox1)
    
    		window.show_all()
    
    myPy()
    gtk.main()
    
  3. 類別相關聯結
    1. gtk.Button class



2017-06-14