/media/sda5/docs/coding/python/pygame/RPG/Client/source 1.1/GUI.py

Go to the documentation of this file.
00001 ## Python Chat and Game server client
00002 ## Written by Bart Spaans, Sep 2007
00003 ## See http://www.onderstekop.nl/coding/ for more scripts
00004 
00005 #GUI libraries;
00006 #See http://www.pygtk.org/ for documentation
00007 import pygtk
00008 pygtk.require('2.0')
00009 import gtk
00010 
00011 #Import protocol to start and stop connections and send messages
00012 from clientProtocol import *
00013 
00014 #Global macros
00015 from clientVars import *
00016 from Log import *
00017 
00018 
00019 ## The Client's Graphical User Interface (GUI) is managed here (PyGTK+2,0)
00020 class GUI:
00021         
00022         ##The constructor class
00023         def __init__(self, msgObject):
00024                 self.messagesmessages = msgObject;
00025                 
00026                 #initiate the container window
00027                 self.windowwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
00028                 self.windowwindow.set_resizable(True)
00029                 self.windowwindow.connect("destroy", self.close_applicationclose_application)
00030                 self.windowwindow.set_title(APP_NAME + " " + APP_VERSION)
00031                 self.windowwindow.set_border_width(20)
00032                 
00033                 #adding widgets
00034                 self.tabletable = gtk.Table(18,4, True)
00035                 self.makeLabelsmakeLabels()
00036                 self.makeEntriesmakeEntries()
00037                 self.makeButtonsmakeButtons()
00038                 self.makeTextAreamakeTextArea()
00039                 self.makeFramesmakeFrames()
00040                 self.windowwindow.add(self.tabletable)
00041                 
00042                 #make the widgets visible
00043                 self.tabletable.show()
00044                 self.windowwindow.show()
00045         
00046         ##Adds a number of labels to the widget table
00047         def makeLabels(self):
00048                 labels = ["Server:", "Port:", "Alias:"]; a = 1
00049                 for label in labels:
00050                         l = gtk.Label(label)
00051                         self.tabletable.attach(l, 0, 1, a, a + 1)
00052                         l.show()
00053                         a = a +1
00054                         
00055         ##Adds a number of entries (textfields) to the widget table.
00056         def makeEntries(self):
00057                 self.hostTexthostText =gtk.Entry()
00058                 self.hostTexthostText.set_text("localhost")
00059                 self.tabletable.attach(self.hostTexthostText, 1, 3, 1, 2)
00060                 self.hostTexthostText.show()
00061                 
00062                 self.portTextportText = gtk.Entry()
00063                 self.portTextportText.set_text("2727")
00064                 self.tabletable.attach(self.portTextportText, 1, 2, 2, 3)
00065                 self.portTextportText.show()
00066                 
00067                 self.aliasTextaliasText = gtk.Entry()
00068                 self.aliasTextaliasText.set_text("Anonymous")
00069                 self.tabletable.attach(self.aliasTextaliasText, 1, 3, 3, 4)
00070                 self.aliasTextaliasText.show()
00071                 
00072                 self.msgTextmsgText = gtk.Entry()
00073                 self.tabletable.attach(self.msgTextmsgText, 0, 3, 17, 18)
00074                 self.msgTextmsgText.show()
00075                 
00076         
00077         ## Add and display Connect and Send buttons
00078         def makeButtons(self):
00079                 b = gtk.Button("Connect")
00080                 b.connect("clicked", self.startServicestartService)
00081                 self.tabletable.attach(b, 2, 3, 2, 3)
00082                 b.show()
00083                 
00084                 b=gtk.Button("Send")
00085                 b.connect("clicked", self.sendMessagesendMessage)
00086                 self.tabletable.attach(b, 3, 4, 17, 18)
00087                 b.show()
00088 
00089         ## Add the chat text area
00090         def makeTextArea(self):
00091                 sw = gtk.ScrolledWindow()
00092                 sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
00093                 sw.set_shadow_type(gtk.SHADOW_IN)
00094                 textview = gtk.TextView()
00095                 textview.set_editable(False)
00096                 textview.set_wrap_mode(gtk.WRAP_WORD)
00097                 self.textbuffertextbuffer = textview.get_buffer()
00098                 self.loglog = Log(self.textbuffertextbuffer, textview)
00099                 sw.add(textview)
00100                 sw.show()
00101                 textview.show()
00102                 self.tabletable.attach(sw, 0, 4, 5, 17)
00103                 
00104         ##Adds a little layout frames
00105         def makeFrames(self):
00106                 f = gtk.Frame("Client options")
00107                 f.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
00108                 f.set_label_align(0.0, 0.0)
00109                 self.tabletable.attach(f, 0, 4, 0, 5)
00110                 f.show()
00111                 
00112                 f= gtk.Frame("Chat")
00113                 f.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
00114                 f.set_label_align(0.0,0.0)
00115                 self.tabletable.attach(f, 0, 4, 5, 18)
00116                 f.show()
00117         
00118         ## Event function that's called when the client has clicked the connect button
00119         def startService(self, widget):
00120                 host = self.hostTexthostText.get_text()
00121                 port = int(self.portTextportText.get_text())
00122                 alias = self.aliasTextaliasText.get_text()
00123                 self.loglog.log("******** PRE-CONNECTION INFO *********");
00124                 self.loglog.log(APP_NAME + " is trying to connect to " + host + " at port " + str(port))
00125                 self.loglog.log("You are logging in under the name " + alias + "\n")
00126                 self.loglog.log("********** CONNECTION INFO ***********");
00127                 runReactor(host, port, self.loglog, alias, self.messagesmessages)
00128                 
00129                 
00130         ## Event function that gets called when the client has clicked the send button
00131         def sendMessage(self, widget):
00132                 msg = self.msgTextmsgText.get_text()
00133                 self.messagesmessages.addmsg(msg)
00134                 self.msgTextmsgText.set_text("")
00135         
00136         ## Function that gets called when the GUI is exited
00137         def close_application(self, widget):
00138                 stopReactor()
00139                 gtk.main_quit()
00140                 
00141 def startGUI(msgObject):
00142         GUI(msgObject)
00143         gtk.main()
00144         return 0

Generated on Mon Oct 8 05:43:00 2007 for Python Game and Chat Client by  doxygen 1.5.1