def __init__(self): gtk.Window.__init__(self) self.msg=MsgFactory() self.set_default_size(400,200) self.set_position(gtk.WIN_POS_CENTER_ALWAYS) self.set_keep_above(True) self.set_decorated(False) self.connect('key-press-event',self.login) self.layout=LayOut(self) self.add(self.layout) #self.fix=gtk.Fixed() #self.fix.set_size_request(400,100) #self.layout.put(self.fix,0,50) label=gtk.Label("用户名") self.layout.put(label,100,55) self.username=gtk.Entry() self.username.set_has_frame(False) self.username.grab_focus() self.username.set_size_request(150,25) self.layout.put(self.username,150,50) label=gtk.Label("密 码") self.layout.put(label,100,105) self.password=gtk.Entry() self.password.set_has_frame(False) self.password.set_size_request(150,25) self.layout.put(self.password,150,100)
def __init__(self,client): gtk.Window.__init__(self) self.user_map={} self.client=client self.set_keep_above(True) self.set_position(gtk.WIN_POS_CENTER) self.set_decorated(False) self.set_size_request(277,597) self.set_resizable(False) self.connect('key-press-event',self.logout) #self.fix=gtk.Fixed() #self.add(self.fix) self.layout=LayOut(self) self.add(self.layout) self.userView=self.create_user_view() self.userModel=self.create_user_model() self.userView.set_model(self.userModel) self.layout.put(self.userView,0,100)
def __init__(self,client,uid,name): gtk.Window.__init__(self) self.client=client self.uid=uid self.current_name='牛敏国' self.set_size_request(545,512) self.set_decorated(False) self.connect('key-press-event',self.sendMsg) self.vbox=gtk.VBox() self.add(self.vbox) self.titleBar=LayOut(self) self.vbox.pack_start(self.titleBar,False,False) vpaned=gtk.VPaned() self.vbox.pack_start(vpaned,False,False) scrolledwindow=gtk.ScrolledWindow() scrolledwindow.set_shadow_type(gtk.SHADOW_ETCHED_IN) scrolledwindow.set_policy(gtk.POLICY_NEVER,gtk.POLICY_AUTOMATIC) vpaned.add1(scrolledwindow) self.msgView=gtk.TextView() self.msgView.set_size_request(-1,300) self.msgView.set_editable(False) scrolledwindow.add(self.msgView) self.sendView=gtk.TextView() self.sendView.set_size_request(-1,300) vpaned.add2(self.sendView) self.bottomBar=LayOut(self) self.bottomBar.setHeight(40) self.vbox.pack_end(self.bottomBar,False,False)
class MainPanel(gtk.Window): def __init__(self,client): gtk.Window.__init__(self) self.user_map={} self.client=client self.set_keep_above(True) self.set_position(gtk.WIN_POS_CENTER) self.set_decorated(False) self.set_size_request(277,597) self.set_resizable(False) self.connect('key-press-event',self.logout) #self.fix=gtk.Fixed() #self.add(self.fix) self.layout=LayOut(self) self.add(self.layout) self.userView=self.create_user_view() self.userModel=self.create_user_model() self.userView.set_model(self.userModel) self.layout.put(self.userView,0,100) #control_button=self.create_control_button() #self.fix.put(control_button,0,560) #search=gtk.Entry() #search.set_size_request(277,30) #search.set_has_frame(False) ##fix.put(search,0,80) #iconview=gtk.IconView() #iconview.set_size_request(277,35) #iconview.set_column_spacing(0) #iconview.set_row_spacing(0) #iconview.set_item_width(92) #iconview.set_text_column(0) #iconview.set_margin(0) #iconview.set_border_width(0) ##iconview.set_item_padding(35) #self.fix.put(iconview,0,70) #iconview.connect('selection-changed',self.selection_changed) ##print gtk.treepath_new_from_string("0") ##path=iconview.get_path_at_pos(0,0) ##iconview.select_path((0,)) #iconview.item_activated((0,)) ##style=iconview.get_style().copy() ##style_fix=self.get_style() ##for state in ( gtk.STATE_NORMAL,gtk.STATE_PRELIGHT,gtk.STATE_ACTIVE): ## style.base[state]=style_fix.bg[gtk.STATE_NORMAL] ##iconview.set_style(style) #model=gtk.ListStore(str) #iconview.set_model(model) #model.append(['我的好友']) #model.append(['最近联系人']) #model.append(['群和讨论组']) def create_user_view(self): treeview=gtk.TreeView() treeview.set_rules_hint(True) treeview.set_headers_visible(False) treeview.connect('row-activated', self.chat_window) treeview.set_size_request(277,450) rendererText=gtk.CellRendererText() rendererText.set_fixed_size(-1,30) column=gtk.TreeViewColumn("",rendererText,text=0) treeview.append_column(column) rendererText=gtk.CellRendererText() column=gtk.TreeViewColumn("",rendererText,text=1) column.set_visible(False) treeview.append_column(column) return treeview def create_user_model(self): treestore=gtk.TreeStore(str,str) return treestore def create_control_button(self): button=gtk.Button("退出") button.connect("clicked",gtk.main_quit) return button def logout(self,widget,event): if event.keyval == 65307: self.destroy() gtk.main_quit() def update(self,gname,users): parent=self.userModel.append(None,[gname,'']) if not users: self.userModel.append(parent,['','']) else: for user in users: self.userModel.append(parent,[user[0],user[1]]) def chat_window(self, treeview, path, column): model=treeview.get_model() treeiter=model.get_iter(path) name=model.get_value(treeiter,0) uid=model.get_value(treeiter,1) msgWindow=MsgWindow(self.client,uid,name) msgWindow.run() self.user_map[uid]=msgWindow def run(self): self.show_all()
class Login(gtk.Window): def __init__(self): gtk.Window.__init__(self) self.msg=MsgFactory() self.set_default_size(400,200) self.set_position(gtk.WIN_POS_CENTER_ALWAYS) self.set_keep_above(True) self.set_decorated(False) self.connect('key-press-event',self.login) self.layout=LayOut(self) self.add(self.layout) #self.fix=gtk.Fixed() #self.fix.set_size_request(400,100) #self.layout.put(self.fix,0,50) label=gtk.Label("用户名") self.layout.put(label,100,55) self.username=gtk.Entry() self.username.set_has_frame(False) self.username.grab_focus() self.username.set_size_request(150,25) self.layout.put(self.username,150,50) label=gtk.Label("密 码") self.layout.put(label,100,105) self.password=gtk.Entry() self.password.set_has_frame(False) self.password.set_size_request(150,25) self.layout.put(self.password,150,100) def run(self): self.show_all() self.connector=reactor.connectTCP(HOST,PORT,ClientFactory(self)) self.transport=self.connector.transport reactor.run() def login(self,widget,event): if event.keyval == 65293: username=self.username.get_text() password=self.password.get_text() self.sendLoginMsg(username,password) if event.keyval == 65307: self.destroy() gtk.main_quit() def sendLoginMsg(self,username,password): Type='CUSTOM_USER_LOGIN' PassWord=md5(password).hexdigest() UserName=username SerialNumber=0 msg=self.msg.packMsg(Type,UserName,PassWord,SerialNumber) print("Sending login-msg ...") self.transport.write(msg) print("Done") for child in self.layout.get_children(): self.layout.remove(child)
class MsgWindow(gtk.Window): def __init__(self,client,uid,name): gtk.Window.__init__(self) self.client=client self.uid=uid self.current_name='牛敏国' self.set_size_request(545,512) self.set_decorated(False) self.connect('key-press-event',self.sendMsg) self.vbox=gtk.VBox() self.add(self.vbox) self.titleBar=LayOut(self) self.vbox.pack_start(self.titleBar,False,False) vpaned=gtk.VPaned() self.vbox.pack_start(vpaned,False,False) scrolledwindow=gtk.ScrolledWindow() scrolledwindow.set_shadow_type(gtk.SHADOW_ETCHED_IN) scrolledwindow.set_policy(gtk.POLICY_NEVER,gtk.POLICY_AUTOMATIC) vpaned.add1(scrolledwindow) self.msgView=gtk.TextView() self.msgView.set_size_request(-1,300) self.msgView.set_editable(False) scrolledwindow.add(self.msgView) self.sendView=gtk.TextView() self.sendView.set_size_request(-1,300) vpaned.add2(self.sendView) self.bottomBar=LayOut(self) self.bottomBar.setHeight(40) self.vbox.pack_end(self.bottomBar,False,False) def run(self): self.show_all() def insertHead(self,color): self.msgView.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse(color)) view_buffer=self.msgView.get_buffer() end=view_buffer.get_end_iter() localtime=time.localtime() current_time='{}:{}:{}'.format(localtime[3],localtime[4],localtime[5]) view_buffer.insert(end,self.current_name + ' ' + current_time + '\n') def insertContent(self,text): self.msgView.modify_text(gtk.STATE_NORMAL, gtk.gdk.color_parse('black')) view_buffer=self.msgView.get_buffer() end=view_buffer.get_end_iter() view_buffer.insert(end,'\t ' + text + '\n') self.msgView.scroll_to_mark(view_buffer.get_insert(),0) def _sendMsg(self,text): Type='CHAT' Body=text Created=int(time.time()) SendGuid=self.client.uid RecvGuid=self.uid msg=self.client.msg.packMsg(Type,Body,Created,SendGuid,RecvGuid) self.client.transport.write(msg) def sendMsg(self,widget,event): if event.keyval == 65293 : _buffer=self.sendView.get_buffer() text=_buffer.get_text(_buffer.get_start_iter(),_buffer.get_end_iter(),include_hidden_chars=False) _buffer.delete(_buffer.get_start_iter(),_buffer.get_end_iter()) self.insertHead('blue') self.insertContent(text) self._sendMsg(text) return True return False def recvMsg(self,sender,msg,sendtime): if sender == 'custom-00001': sender="胡咺晖" elif sender == 'custom-00003': sender="黄艳英" elif sender == 'custom-00002': sender="聂桂莹" self.insertHead('blue') self.insertContent(msg)