Want more features on Pastebin? Sign Up, it's FREE!

wxChat.py

By: Dernhelm on Jul 5th, 2012  |  syntax: Python  |  size: 5.02 KB  |  views: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import wx
  2. from twisted.internet import wxreactor
  3. wxreactor.install()
  4. from twisted.spread import pb
  5. from twisted.internet import reactor, threads
  6. from twisted.cred import credentials
  7. import getpass
  8.  
  9. class Client(pb.Referenceable):
  10.     def connect(self):
  11.             #who = str(getpass.getuser()).upper()
  12.         name = "USER" #raw_input("Username: ")
  13.         client = self
  14.         app = wx.App(False)
  15.         frame = Interface()
  16.         factory = pb.PBClientFactory(frame)
  17.         from twisted.internet import reactor #test if this is really required (and why?!)
  18.         reactor.registerWxApp(app)
  19.         reactor.connectTCP("localhost", 1025, factory)
  20.         def1 = factory.login(credentials.UsernamePassword(name, "none"), client)
  21.         def1.addCallback(client.connection)
  22.         reactor.run()
  23.  
  24.     def connection(self, userObj):
  25.         #recieve and store avatar object remote reference
  26.         self.user = userObj #the user object for this client instance...keep around!
  27.         #print "got perspective/user ref:", self.user
  28.         self.returnUser()
  29.  
  30.     def returnUser(self):
  31.         print self.user
  32.         return self.user
  33.  
  34.     def send(self, message, group):
  35.         group.callRemote('send', message)
  36.  
  37. class Interface(wx.Frame):
  38.     def __init__(self):
  39.         wx.Frame.__init__(self, None, -1, "Test Chat", size = (300, 400))
  40.         panel = wx.Panel(self)
  41.         notebook = Notebook(panel)
  42.         sizer = wx.BoxSizer(wx.VERTICAL)
  43.         sizer.Add(notebook, 1, wx.EXPAND, 5)
  44.         panel.SetSizer(sizer)
  45.         self.Layout()
  46.         self.Show()
  47.  
  48. class Notebook(wx.Notebook):
  49.     def __init__(self, parent):
  50.         wx.Notebook.__init__(self, parent, -1, size = (280, 380), name = 'Chat')        
  51.         chat = ChatBox(self)
  52.         user = UserPanel(self)
  53.         group = GroupPanel(self)
  54.         self.AddPage(chat, 'Chat')
  55.         self.AddPage(user, 'Users')
  56.         self.AddPage(group, 'Groups')
  57.         sizer = wx.BoxSizer()
  58.         sizer.Add(self, 0, wx.EXPAND)
  59.         self.SetSizer(sizer)
  60.         self.Layout()
  61.  
  62. #creates a space for notebook chat pages
  63. class ChatBox(wx.Notebook):
  64.     def __init__(self, parent):
  65.         wx.Notebook.__init__(self, parent, -1, size = (280, 380), style = wx.NB_RIGHT, name = 'Chat Box')
  66.         groupN = "Example"
  67.  
  68.         #use this to make a new tab for each new chat:
  69.         self.newChat(groupN)
  70.        
  71.         sizer = wx.BoxSizer()
  72.         sizer.Add(self, 0, wx.EXPAND)
  73.         self.SetSizer(sizer)
  74.         self.Layout()
  75.     def newChat(self, groupN):
  76.         chat = ChatPanel(self)
  77.         self.AddPage(chat, groupN)
  78.    
  79.     #add readonly and message entry textboxes to every chat tab
  80. class ChatPanel(wx.Panel):
  81.     def __init__(self, parent):
  82.         wx.Panel.__init__(self, parent = parent, id=wx.ID_ANY)
  83.         sizer = wx.BoxSizer(wx.VERTICAL)
  84.         self.text = wx.TextCtrl(self, style = wx.TE_MULTILINE | wx.TE_READONLY)
  85.         self.ctrl = wx.TextCtrl(self, style = wx.TE_PROCESS_ENTER, size=(200, 50))
  86.         sizer.Add(self.text, 5, wx.EXPAND)
  87.         sizer.Add(self.ctrl, 0, wx.EXPAND)
  88.         self.SetSizer(sizer)
  89.         self.ctrl.Bind(wx.EVT_TEXT_ENTER, self.send)
  90.         self.Layout()
  91. #      connect to client's remote_print and write methods
  92.  
  93.     def send(self, event):
  94.         message  = str(self.ctrl.GetValue())
  95.         print message
  96.         #send to server
  97.         client.send(message)
  98.         self.ctrl.SetValue("")
  99.  
  100. class UserPanel(wx.Panel):
  101.     def __init__(self, parent):
  102.         wx.Panel.__init__(self, parent = parent, id = wx.ID_ANY)
  103.         sizer = wx.BoxSizer(wx.VERTICAL)
  104.         uList = ['test', 'another', 'something', 'else'] #client.returnUL()
  105.         #self.userList = ['test', 'another', 'one more'] #ChatC().user.callRemote('userList')
  106.         self.listbox = wx.ListBox(self, -1, size =(270, 380),  style = wx.LB_HSCROLL)        
  107.         for userN in uList:
  108.             self.listbox.Append(userN)
  109.         sizer.Add(self.listbox, 0, wx.ALL|wx.EXPAND, 5)
  110.         self.SetSizer(sizer)
  111.         self.Layout()
  112.  
  113. class GroupPanel(wx.Panel):
  114.     def __init__(self, parent):
  115.         wx.Panel.__init__(self, parent = parent, id = wx.ID_ANY)
  116.         sizer = wx.BoxSizer(wx.VERTICAL)
  117.         #get group list from server
  118.         gList = ['test', 'another', 'something', 'else'] # client.returnGL()
  119.         #groupList = ['something', 'else', 'other']
  120.         self.listbox = wx.ListBox(self, -1, size =(270, 380),  style = wx.LB_HSCROLL)  
  121.         for groupN in gList:
  122.             self.listbox.Append(groupN)
  123.         sizer.Add(self.listbox, 0, wx.ALL|wx.EXPAND, 5)
  124.         self.SetSizer(sizer)
  125.         self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnClick, self.listbox)
  126.         self.Layout()
  127.        
  128.     #does not work: TODO
  129.     def OnClick(self, event):
  130.         #get the name [groupN]
  131.         groupN = event.GetText()
  132.         #call joinGroup (server) with that name
  133.         #add a tab to the ChatBox [ChatBox.newChat(groupN)]
  134.         parent.chat.newChat(groupN)
  135.  
  136.        
  137. if __name__ == "__main__":
  138.     Client().connect()
clone this paste RAW Paste Data