ผมยังมือใหม่อยู่ เลยเอาตัวอย่างมาลองแก้ไขดู อาจจะมีประโยชน์สำหรับผู้ที่กำลังศึกษาอยู่บ้างนะคับ
#!/usr/bin/env python # example-start multiplewindows2.py import pygtk pygtk.require('2.0') import gtk # Create a new hbox with an image and a label packed into it # and return the box. def xpm_label_box(parent, xpm_filename, label_text): # Create box for xpm and label box1 = gtk.HBox(False, 0) box1.set_border_width(2) # Now on to the image stuff image = gtk.Image() image.set_from_file(xpm_filename) # Create a label for the button label = gtk.Label(label_text) # Pack the pixmap and label into the box box1.pack_start(image, False, False, 3) box1.pack_start(label, False, False, 3) image.show() label.show() return box1 class MultipleWindows: def delete_event(self, widget, event, data=None): # If you return FALSE in the "delete_event" signal handler, # GTK will emit the "destroy" signal. Returning TRUE means # you don't want the window to be destroyed. # This is useful for popping up 'are you sure you want to quit?' # type dialogs. print "delete event occurred" self.window2.hide() # Change FALSE to TRUE and the main window will not be destroyed # with a "delete_event". return True # Our usual callback method def callback(self, widget, data=None): if self.childwin != 0: self.window2.show() return self.window2 = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window2.set_title("Window2") # It's a good idea to do this for all windows. self.window2.connect("destroy", lambda wid: gtk.main_quit()) self.window2.connect("delete_event", self.delete_event) # *** define our own respond self.window2.set_size_request(200,300) # Sets the border width of the window. self.window2.set_border_width(10) table = gtk.Table(3,6,False) self.window2.add(table) # Create a new notebook, place the position of the tabs notebook = gtk.Notebook() notebook.set_tab_pos(gtk.POS_TOP) table.attach(notebook, 0,6,0,1) notebook.show() self.show_tabs = True self.show_border = True # Let's append a bunch of pages to the notebook for i in range(5): bufferf = "Append Frame %d" % (i+1) bufferl = "Page %d" % (i+1) frame = gtk.Frame(bufferf) frame.set_border_width(10) frame.set_size_request(100, 75) frame.show() label = gtk.Label(bufferf) frame.add(label) label.show() label = gtk.Label(bufferl) notebook.append_page(frame, label) # Set what page to start at (page 4) notebook.set_current_page(3) table.show() self.window2.show() self.childwin += 1 def __init__(self): # Create a new window self.childwin = 0 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("Image'd Buttons!") # It's a good idea to do this for all windows. self.window.connect("destroy", lambda wid: gtk.main_quit()) self.window.connect("delete_event", lambda a1,a2:gtk.main_quit()) # Sets the border width of the window. self.window.set_border_width(10) # Create a new button button = gtk.Button() # Connect the "clicked" signal of the button to our callback button.connect("clicked", self.callback, "cool button") # This calls our box creating function box1 = xpm_label_box(self.window, "soccerball.gif", "cool button") # Pack and show all our widgets button.add(box1) box1.show() button.show() self.window.add(button) self.window.show() childwin = 0 def main(): gtk.main() return 0 if __name__ == "__main__": MultipleWindows() main()
กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น
ลองดูฟังก์ชันที่สำคัญทีละบรรทัดนะคับ โปรแกรม รันมาตอนแรกจะมีปุ่มให้กดนะคับ แล้ววินโดสว์อีกอันจะขึ้นมา
ใน blockcode ใส่ type="python" ด้วยก็ดีนะครับ จะได้มีสี