next up previous contents
Next: 實機練習題 Up: PyGTK Previous: Signals, Events and Functions   Contents

Label and Entry

  1. Example1: Label
    #!/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":
    			self.label.set_text("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)
    		self.label=gtk.Label("Yes|No")
    		hbox1.add(yes)
    		hbox1.add(no)
    		hbox2.add(close)
    		hbox3.add(self.label)
    		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: Entry
    #!/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":
    			self.label.set_text(data)
    		else:
    			self.label.set_text("Only yes|no")
    
    	def entryChange(self,widget):
    		if self.entry.get_text() == "yes":
    			subprocess.Popen("gnome-terminal",shell=True)
    			self.label.set_text("yes")
    		elif self.entry.get_text() == "no":
    			self.label.set_text("no")
    		else:
    			self.label.set_text("Only 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)
    		self.label=gtk.Label("Yes|No")
    		self.entry=gtk.Entry()
    		close.connect("clicked", lambda x: gtk.main_quit())
    		yes.connect("clicked", self.callback, 'yes')
    		no.connect("clicked", self.callback, 'no')
    		self.entry.connect("changed", self.entryChange)
    		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)
    		hbox3.add(self.label)
    		hbox3.add(self.entry)
    		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()
    
    if __name__ == "__main__":
    	print __name__
    	myPy()
    	gtk.main()
    else:
    	print __name__
    
  3. 類別相關聯結
    1. gtk.Label class
    2. gtk.Entry class



2017-06-14