CSC 15 Optional Lab : Gui Construction For this lab you are to create a gui-program that converts a phone number with letters into a purely numerical number. It should resemble the sample program I showed you in class. Here are some additional hints on how to do that: 1. Create a class called phonecode: The first thing you should do is to write the PURELY COMPUTATIONAL code to covert a character into a phone number digit. You can do this by either writing a function: def convert(self,A): that returns a character representing the conversion (or return A unchanged if it's not an alphabetical character). Think about using an array or hash table instead of too many if-else statements. 2. The gui setup is mostly done in __init__: You'll need both a gtk.Hbox and a gtk.Vbox. The main box that's added to your window should be a Vbox. The Hbox should pack 10 gtk.Entry objects plus 2 gtk.Label objects (for the 2 dashes). The Vbox should pack the Hbox plus two buttons, one to convert the code and the other to clear the entries. Given a gtk.Entry E: E.set_text("abc") # sets the text to "abc", use "" to clear t = E.get_text() # returns text found in entry E E.set_size_request(50,50) # manually set the size of the "widget" E.grab_focus() # moves input cursor to the entry E.modify_font(pango.FontDescription("Sans Bold 24")) # sets font Many of these functions can also be called on buttons and other widgets. You can use set_size_request on most gui items. But to change the text of buttons you need to call set_label instead. Experiment, and consult the online tutorial and API reference for help. *** IMPORTANT: don't create 10 variables, one for each entry: USE AN ARRAY! *** Follow the format of the sample program that I gave you. Don't forget to: add the Vbox to the window call show_all on the window connect the bottons to event handling routines Allow window closing to terminate the program (see sample program) 3. Now you need to write two "callback" functions that react to button clicks The convert button's function is to go through your array of gtk.Entry objects, get the text, check that it's not "", convert it, and reset the text. The clear button should just set the text of each entry to "". 4. You'll notice that you have to type tab to move to the next Entry field. It would be nice if this is done automatically after each character entry. To do this you'll need a callback function that can respond to keyboard events: # the following function is called whenever a key is pressed: def keyhandler(self,widget,Kevent): k = Kevent.keyval # value of key print "key with code",k," was just pressed" # keyhandler # connects key-press event to calling the above function: handlerid = self.window.connect("key_press_event", keyhandler) Alternatively, instead of connecting "key_press_event" to the entire self.window, you can try to catch the "insert-at-cursor" event on each gtk.Event object. That is, when you press enter on an Entry it raises the "activate" event, but whenever you insert new text, it raises the "insert-at-cursor" event. Consult the reference documentation on the Event class for further details. You may also try connecting the keyhandler to EACH entry object instead of the entire window. You'll also need to find a way to keep track of which Entry you just typed in. Call grab_focus() on an entry to move the cursor there. 5. *** Challenge: In my program I couldn't make the clear button smaller somehow. Figure out how and impress me with your tenacity. ############################################################################ 6. *** MEGA-Challenge: Texting You can detect the approximate time between clicks using the time.time() call: import time # at the top of your program print time.time() # prints the current time as a floating point numbers. print time.clock() # similar: use this if time.time() does work on windows. With this functionality you should be able to figure out how to detect double, triple-clicks. Your goal is to write a gui program that simulates texting. You should have 10 (or more buttons) plus a text entry on top. The buttons, labeled 0-9, should be arranged as on a phone's keypad. The text entry on top should display what you've texted. Additional buttons should be added to cancel/clear, etc... If an invalid key is clicked your gui should behave as your phone does.