Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-----------------------------[Section 2: Communications]
- # Defining Variables
- # Outgoing Data Buffer
- out_buffer = []
- # Incoming Data Function
- def incoming():
- rlist,wlist,xlist = select.select([s], out_buffer, [])
- print("listening")
- while 1:
- for i in rlist:
- data = i.recv(1024)
- if data:
- reply_decode = data.decode()
- reply_processed = json.loads(reply_decode)
- # Message Decoding
- Reply_Trans = []
- y = 0
- Reply_Limit = len(reply_processed)
- while y < Reply_Limit:
- try:
- Reply_Trans = Reply_Trans + [R_Dictionary[reply_processed[y]]]
- except ValueError:
- tkinter.messagebox.showinfo("Internal Error","Section 3: IDF ValueError")
- y += 1
- Reply_Combine = "".join(str(p) for p in Reply_Trans)
- print("Reply_Combine")
- chatbox.insert(INSERT,"Client: "+Reply_Combine+ "\n")
- if numlines > 15:
- chatbox.delete("1.0",END)
- chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
- INPUT_BOX.set("")
- # Outgoing Data Function
- def outgoing():
- global out_buffer
- try:
- user_input = USER_ENTRY.get()
- chatbox.insert(INSERT,"You: "+USER_ENTRY.get()+ "\n")
- user_lower = user_input.lower()
- Mess_Trans = [] # Holds Translated message
- x = 0
- Count_Limit = len(user_lower)
- while x < Count_Limit:
- try:
- Mess_Trans = Mess_Trans + [T_Dictionary[user_lower[x]]]
- except ValueError:
- print("Internal Error Exception")
- tkinter.messagebox.showinfo("Internal Error","Dictionary Failed To Encrypt")
- x += 1
- # Serliazing List Before Exporting
- Mess_Seri = json.dumps(Mess_Trans)
- # Exporting Message
- if user_input:
- out_buffer += [Mess_Seri.encode()]
- # for i in wlist:
- s.send(out_buffer[0])
- out_buffer = []
- print("Sending..")
- numlines = len(chatbox.get("1.0",END).splitlines())
- if numlines > 15:
- chatbox.delete("1.0",END)
- chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
- INPUT_BOX.set("")
- except IOError:
- print("\n\t\aIOError Exception")
- tkinter.messagebox.showinfo("Internal Error","Section 3: IOError on send attempt")
- input("\n\nPress Enter To Exit")
- # ----------------------------[ Section 2, GUI Chatbox ]
- # Setting Variables
- global textlimiter
- textlimiter = 0
- # Defining Functions
- # Chat Control and Command
- def CONVERT():
- numlines = len(chatbox.get("1.0",END).splitlines())
- if numlines > 15:
- chatbox.delete("1.0",END)
- chatbox.insert(INSERT,"Console: Limit Reached, Chat Wiped" + "\n")
- INPUT_BOX.set("")
- def chatexit():
- time.sleep(0.5)
- sys.exit()
- # Starting Incoming.
- def bicommand():
- TI = 0
- outgoing()
- if TI == 0:
- thread_in = threading.Thread(target=incoming, args=())
- thread_in.start()
- #thread_in.start() # this causes the thread to run
- #thread_in.join doesn't seem to affect the freeze....
- #thread_in.join() # this waits until the thread has completed
- # Chatbox Window
- chat = Tk()
- chat.title("Net Send Client [0.5]")
- chat.geometry('550x500+200+200')
- #
- title = StringVar()
- title.set("Net Send Chat\n Type in box to send.")
- title_widget = Label(chat,textvariable = title, height = 4)
- title_widget.pack()
- #
- INPUT_BOX = StringVar()
- USER_ENTRY = Entry(chat,textvariable = INPUT_BOX)
- USER_ENTRY.pack(side=TOP, padx = 10, pady = 20)
- #
- send_button = Button(chat, text="Send",width = 20, command = bicommand)
- send_button.pack(padx = 10, pady = 10)
- #
- chatbox = Text(width = 60, height = 15, relief = SUNKEN)
- left = Frame(chatbox)
- right = Frame(chatbox)
- s_start = Scrollbar(right)
- s_start.pack(side=RIGHT)
- #
- exit_button = Button(chat, text="Close & Exit",width = 20, command = chatexit)
- exit_button.pack(side='bottom',padx = 15, pady = 14)
- #
- chatbox.grid(row = 0, column = 0, columnspan = 3)
- chatbox.pack()
- #
- chat.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment