def __init__(self, master, callback, dest_user=None): self.dest_user = dest_user self.display = ttk.Frame(master) self.display.pack(side='top') scrollbar = ttk.Scrollbar(self.display) self.text_display = ReadOnlyText(self.display, width=45, height=36, yscrollcommand=scrollbar.set, wrap='word', borderwidth=0) scrollbar.config(command=self.text_display.yview) # text input area self.textinput = tkinter.Text(self.display, width=35, height=10, wrap='word') self.textinput.bind('<Control_L><Return>', callback) if not dest_user: # if no dest_user (Welcome window), disable textinput self.textinput.insert(tkinter.END, 'Select a peer first') self.textinput.config(state='disabled') else: self.textinput.insert(tkinter.END, 'Please type') # pack self.textinput.pack(side='bottom', fill='both', expand=True) scrollbar.pack(side='right', fill='y') self.text_display.pack(side='left', fill='both', expand=True)
class ChatDisplay(object): def __init__(self, master, callback, dest_user=None): self.dest_user = dest_user self.display = ttk.Frame(master) self.display.pack(side='top') scrollbar = ttk.Scrollbar(self.display) self.text_display = ReadOnlyText(self.display, width=45, height=36, yscrollcommand=scrollbar.set, wrap='word', borderwidth=0) scrollbar.config(command=self.text_display.yview) # text input area self.textinput = tkinter.Text(self.display, width=35, height=10, wrap='word') self.textinput.bind('<Control_L><Return>', callback) if not dest_user: # if no dest_user (Welcome window), disable textinput self.textinput.insert(tkinter.END, 'Select a peer first') self.textinput.config(state='disabled') else: self.textinput.insert(tkinter.END, 'Please type') # pack self.textinput.pack(side='bottom', fill='both', expand=True) scrollbar.pack(side='right', fill='y') self.text_display.pack(side='left', fill='both', expand=True) def getdestuser(self): return self.dest_user def getframe(self): return self.display def is_sysmsg(self, text): # check if it is system message try: if text.index('SYS') == 0: return True except ValueError: pass return False def add_text(self, text, mine=False): text = text.strip(' \n') if self.dest_user: if self.is_sysmsg(text): author = '' elif mine: author = 'I said:\n' else: author = self.dest_user + ' said:\n' text = author + text + '\n\n\n' else: # welcome messages do not have user_id text = text + '\n\n' self.text_display.insert(tkinter.END, text) def poll(self): ''' Return the text in its textinput Also add the text to its own display window ''' textstr = self.textinput.get(1.0, tkinter.END) self.add_text(textstr, mine=True) self.textinput.delete(1.0, tkinter.END) return textstr