# a sample graphical user interface using python-gtk import pygtk import gtk import pango # independent function to add new text to the end of a gtk.TextView: def addtext(TV,text): buffer = TV.get_buffer() iter = buffer.get_iter_at_mark(buffer.get_insert()) buffer.insert(iter,text) # use "\n" for newlines # addtext class gui1: # The init function will set up the graphical layout of the gui: def __init__(self,x,y): # init with x,y position of window self.width, self.height = 200,100 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # create window object self.window.set_title("gui1 window") # self.window.resize(self.width,self.height) # best leave alone # The principal "box" containing gui elements self.mainbox = gtk.VBox(False,20) # 20 pixels between boxes self.window.add(self.mainbox) # add box container to window # box to hold gui elements except menu bar self.box1 = gtk.HBox(False, 0) # a verticle "box" of widgets # Create GUI elements: self.button1 = gtk.Button("OK") # create button objects self.button2 = gtk.Button("Cancel") self.text1 = gtk.Entry(32) # text entry field with max length self.check = gtk.CheckButton("Always trust contents from Prof. Liang") self.check.set_active(True) # initially checked self.label1 = gtk.Label("this is a label:") # A multi-line text area must be first added to a scrollwindow: sw = gtk.ScrolledWindow() self.textarea = gtk.TextView() sw.add(self.textarea) sw.set_size_request(300,150) # can use on any "widget" # create vertical box for the textview and the label self.box2 = gtk.VBox(False,0) self.box2.pack_start(self.label1,True,True,5) self.box2.pack_start(sw, True, True, 5) self.box2.pack_start(self.check,True,True,5) # "Pack" gui elements into the container "box": self.box1.pack_start(self.button1, True, True, 5) self.box1.pack_start(self.button2, True, True, 5) self.box1.pack_start(self.text1, True, True, 5) self.box1.pack_start(self.box2, True, True, 5) # self.box1.pack_start(sw, True, True, 5) # connect events with "callback" functions: self.button1.connect("clicked",self.handlerb1) self.button2.connect("clicked",self.handlerb2) self.check.connect("toggled",self.checkhandler) self.window.connect("destroy", lambda w: self.window.destroy()) # self.window.connect("destroy", lambda w: gtk.main_quit()) ##### Create a menu bar with "file" and "help" menus self.menubar = gtk.MenuBar() # Create separate menus self.fmenu = gtk.Menu() self.hmenu = gtk.Menu() #Menu items: self.saveitem = gtk.MenuItem("Save") self.quititem = gtk.MenuItem("Quit") self.infoitem = gtk.MenuItem("About") # Add menu items to menus: self.fmenu.append(self.saveitem) self.fmenu.append(self.quititem) self.hmenu.append(self.infoitem) # Add menus to menu bar: # But this also involves labeling the menus on the menubar: filelabel = gtk.MenuItem("File") helplabel = gtk.MenuItem("Help") filelabel.set_submenu(self.fmenu) helplabel.set_submenu(self.hmenu) self.menubar.append(filelabel) self.menubar.append(helplabel) # note that filelabel is a local var - this is ok, since it # won't be referred to outside of init # Add the entire menubar to the window's main box self.mainbox.pack_start(self.menubar,True,True,5) self.mainbox.pack_start(self.box1,True,True,5) # Connect menu items with callback event handlers self.saveitem.connect("activate",self.menuhandler,"save") self.quititem.connect("activate",self.menuhandler,"quit") self.infoitem.connect("activate",self.menuhandler,"info") ##### finally done with the menu! self.text1.modify_font(pango.FontDescription("Sans 12")) #set font self.text1.grab_focus() # set curor to this widget ######################### Show and move main window self.window.show_all() # displays window and all components self.window.move(x,y) # moves window to screen coordinates # move must be called after show_all # init # event handler for the "OK" button: def handlerb1(self,widget): # first 2 params are required self.text1.set_text("No, it's not OK") addtext(self.textarea,"hello there!\n") # handlerb1 def handlerb2(self,widget): # handler for the "cancel" button self.text1.set_text("Too late to cancel") # self.text1.get_text() will return text found in entry # handlerb2 def checkhandler(self,widget): # handler for the checkbox state = self.check.get_active() if state==False: addtext(self.textarea,"Boo!\n") #checkhandler def menuhandler(self,widget,source): # handler for menu if source=="save": response = "You've done nothing worth saving.\n" if source=="quit": response = "Why would you want to quit such a nice program?\n" if source == "info": response = "This is a gui program, can't you tell!\n" addtext(self.textarea,response) #menuhandler # gui1 class # create instances of gui1 class mygui = gui1(0,0) yourgui = gui1(400,400) # The following line starts the gtk "event loop" gtk.main()