# Graphical interface for scramble server program import pygtk import gtk import pango # independent function to add new text to the end of a gtk.TextView object: 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 TV.scroll_to_iter(iter,0.1) # addtext class servergui: def __init__(this,port): this.width, this.height = 512,640 # window dimensions this.window = gtk.Window(gtk.WINDOW_TOPLEVEL) this.window.set_title("Scrambler Server Interface") this.window.connect("destroy", lambda w: gtk.main_quit()) # define principal vertical box mainvbox = gtk.VBox(False, 5) # homogenous spacing, pixels between... this.window.add(mainvbox) # add box container to window # a small horizontal boxes for buttons and checkboxes hbox1 = gtk.HBox(False,32) # can be local var if not referred to outside hbox2 = gtk.HBox(False,32) # textview to display message from student + label this.toplabel = gtk.Label("Waiting for connection...") this.textarea = gtk.TextView() this.textarea.set_size_request(500,400) this.textarea.set_editable(False) # buttons to optionally select scramble 1,2 this.s1button = gtk.Button("Descramble1") this.s2button = gtk.Button("Descramble2") # pack buttons into small hbox hbox1.pack_start(this.s1button,True,True,5) hbox1.pack_start(this.s2button,True,True,5) # text entry for professor's response to students this.entry = gtk.Entry(256) # 256 is size in chars # 2 checkboxes this.s2check = gtk.CheckButton("Advanced Scramble") this.filecheck = gtk.CheckButton("Send File") # pack checkboxes into hbox2 hbox2.pack_start(this.s2check,True,True,5) hbox2.pack_start(this.filecheck,True,True,5) # pack everything into main verticle box mainvbox.pack_start(this.toplabel,True,True,5) mainvbox.pack_start(this.textarea,True,True,5) mainvbox.pack_start(hbox1,True,True,5) mainvbox.pack_start(this.entry,True,True,5) mainvbox.pack_start(hbox2,True,True,5) #### this sets the visual attributes, now set event handlers this.s1button.connect("clicked",this.descram) this.s2button.connect("clicked",this.descram) this.entry.connect("activate",this.sender) # other attributes this.entry.modify_font(pango.FontDescription("Sans 12")) #set font this.window.show_all() # actually displays window #__init__ ##### event handler routines def descram(this,widget): # can handle either descram button if widget==this.s1button: addtext(this.textarea,"Using simple descramble...\n") if widget==this.s2button: addtext(this.textarea,"Using advanced descramble...\n") #descram # this function is called when prof hits enter on the text Entry area def sender(this,widet): # check check boxes to determine how to interpret text s2checked = this.s2check.get_active() filechecked = this.filecheck.get_active() if s2checked: addtext(this.textarea,"Sending using advanced scramble\n") else: addtext(this.textarea,"Sending using simple scramble\n") if filechecked: addtext(this.textarea,"Sending scrambled file...\n") else: addtext(this.textarea,"Sending scrambled text to sucker...\n") # clear text this.entry.set_text("") # get_text() will return text as string # sender #servergui class # create instance of servergui class to listen on port: sg1 = servergui(12004) addtext(sg1.textarea,"received lame joke from student...\n") # can create second instance of class to listen on different port # sg2 = servergui(12005) # must be last line: gtk.main()