python手记(6)

#!/usr/bin/env python
# example table.py
import pygtk
pygtk.require("2.0")
import gtk
class Table:
	# Our callback.
	# The data passed to this method is printed to stdout
	def callback(self, widget, data=None):
		print "Hello again - %s was pressed" % data
		# This callback quits the program
	def delete_event(self, widget, event, data=None):
		gtk.main_quit()
		return False
	def __init__(self):
		# Create a new window 
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		# Set the window title
		self.window.set_title("Table")
		# Set a handler for delete_event that immediately
		# exits GTK.
		self.window.connect("delete_event", self.delete_event)
		# Sets the border width of the window.
		self.window.set_border_width(20)
		# Create a 2x2 table
		table = gtk.Table(2, 2, True)
		# Put the table in the main window
		self.window.add(table)
		# Create first button
		button = gtk.Button("button 1")
		# When the button is clicked, we call the "callback" method
		# with a pointer to "button 1" as its argument
		button.connect("clicked", self.callback, "button 1")
		# Insert button 1 into the upper left quadrant of the table
		table.attach(button, 0, 1, 0, 1)
		button.show()
		# Create second button
		button = gtk.Button("button 2")
		# When the button is clicked, we call the "callback" method
		# with a pointer to "button 2" as its argument
		button.connect("clicked", self.callback, "button 2")
		# Insert button 2 into the upper right quadrant of the table
		table.attach(button, 1, 2, 0, 1)
		button.show()
		# Create "Quit" button
		button = gtk.Button("Quit")
		# When the button is clicked, we call the main_quit function
		# and the program exits
		button.connect("clicked", lambda w: gtk.main_quit())
		# Insert the quit button into the both lower quadrants of the table
		table.attach(button, 0, 2, 1, 2)
		button.show()
		table.show()
		self.window.show()
def main():
        gtk.main()
        return 0
if __name__ == "__main__":
	Table()
	main() 
 

本博客所有内容是原创,未经书面许可,严禁任何形式的转

http://blog.csdn.net/u010255642


pygtk表格 

table = gtk.Table(rows=1, columns=1, homogeneous=False)

第一个参数是表格的行数,第二个是表格的列数,homogeneous表格的box的大小是否调整成最大的widget的大小。rows=2且colunms=2的表格如下:

 

0               1              2

0+----------+----------+

|

|

|

1+----------+----------+

|

|

|

2+----------+----------+

在表格中放置widget

 

table.attach(child, left_attach, right_attach, top_attach, bottom_attach,
xoptions=EXPAND|FILL, yoptions=EXPAND|FILL, xpadding=0, ypadding=0)

假设你要在一个2*2的表格中,在首行放置widget,则 left_attach = 0,right_attach = 2, top_attach = 0,bottom_attach = 1。x和y字义了放置的选项,可以使用or定义多选项:
FILL表示如果表格单元比widget大一点,widget 在单元中将填充整个房间
SHRINK 表示如果表格widget被分配小些的空间,widgets在窗体的底部被放置和消失
EXPAND导致表格单元被扩展到表格的余下空间
 set_row_spacing() 和set_col_spacing() 在特定的行或列中加空间
table.set_row_spacing(row, spacing)
table.set_col_spacing(column, spacing)
在所有行列中设置加固定空间
table.set_row_spacings(spacing)
table.set_col_spacings(spacing)
此外,在win下关闭窗口要注意:

1.关闭按钮的事件如下:

button.connect_object("clicked", gtk.Widget.destroy,window)

2.编写一个destroy的处理函数

 def close_application(self, widget):
  gtk.main_quit()

3.在__init__中设置处理窗口destroy的函数

  window.connect("destroy", self.close_application)


我们将上面的代码修改后可以成功退出窗口

#!/usr/bin/env python
# example table.py
import pygtk
pygtk.require("2.0")
import gtk
class Table:
        # Our callback.
        # The data passed to this method is printed to stdout
        def callback(self, widget, data=None):
                print "Hello again - %s was pressed" % data
                # This callback quits the program
        def delete_event(self, widget, event, data=None):
                gtk.main_quit()
                return False
        def close_application(self, widget):
                gtk.main_quit()
        def __init__(self):
                # Create a new window 
                self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
                # Set the window title
                self.window.set_title("Table")
                # Set a handler for delete_event that immediately
                # exits GTK.
                self.window.connect("delete_event", self.delete_event)
                # Sets the border width of the window.
                self.window.set_border_width(20)
                # Create a 2x2 table
                table = gtk.Table(2, 2, True)
                # Put the table in the main window
                self.window.add(table)
                # Create first button
                button = gtk.Button("button 1")
                # When the button is clicked, we call the "callback" method
                # with a pointer to "button 1" as its argument
                button.connect("clicked", self.callback, "button 1")
                # Insert button 1 into the upper left quadrant of the table
                table.attach(button, 0, 1, 0, 1)
                button.show()
                # Create second button
                button = gtk.Button("button 2")
                # When the button is clicked, we call the "callback" method
                # with a pointer to "button 2" as its argument
                button.connect("clicked", self.callback, "button 2")
                # Insert button 2 into the upper right quadrant of the table
                table.attach(button, 1, 2, 0, 1)
                button.show()
                # Create "Quit" button
                button = gtk.Button("Quit")
                # When the button is clicked, we call the main_quit function
                # and the program exits
                button.connect_object("clicked", gtk.Widget.destroy,self.window)
                # Insert the quit button into the both lower quadrants of the table
                table.attach(button, 0, 2, 1, 2)
                self.window.connect("destroy", self.close_application)
                button.show()
                table.show()
                self.window.show()
                
def main():
        gtk.main()
        return 0
if __name__ == "__main__":
        Table()
        main() 
 


你可能感兴趣的:(python,pygtk)