Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
GroupD3/GUI.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
111 lines (94 sloc)
4.02 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
import socket | |
from fileCheck import * | |
client = socket.socket() # Connect to IP:Port | |
port = 12346 | |
client.connect(('localhost', port)) | |
FirstEntry = True | |
userName = "" | |
def pressAction(event): | |
entryBox.config(state=NORMAL) | |
clickAction() | |
def disableEntry(event): | |
entryBox.config(state=DISABLED) | |
def clickAction(): | |
try: | |
global FirstEntry | |
global userName | |
if FirstEntry == True: # When you send the first message | |
FirstEntry = False | |
userName = entryBox.get("0.0", END)[:-1] | |
inDatabase = readFunction(userName) | |
if inDatabase == False: | |
savePreferences(userName) | |
entryText = filteredMessage(entryBox.get("0.0", END)) | |
loadMyEntry(chatDispl, entryText) | |
if not(entryText == ''): | |
client.send( entryText.encode('utf-8') ) # Encode the messsage in bits and send it to server | |
try: | |
rawmsg = client.recv(1024) # recieve up to 1024 bytes | |
msg = rawmsg.decode('utf-8').rstrip('\r\n') # Decode received message from bits to string | |
loadOtherEntry(chatDispl, msg) | |
except: | |
print("Didnt receive a message") | |
chatDispl.yview(END)#Scroll to the bottom of chat windows | |
entryBox.delete("0.0", END) #Erace previous message in Entry Box | |
except: | |
client.shutdown(1) | |
print("Shutdown") | |
def filteredMessage(entryText): | |
""" | |
Filter out all useless white lines at the end of a string, | |
returns a new. | |
""" | |
endFiltered = '' | |
for i in range(len(entryText)-1,-1,-1): | |
if entryText[i]!='\n': | |
endFiltered = entryText[0:i+1] | |
break | |
for i in range(0,len(endFiltered), 1): | |
if endFiltered[i] != "\n": | |
return endFiltered[i:]+'\n' | |
return '' | |
def loadMyEntry(chatLog, entryText): | |
if entryText != '': | |
chatLog.config(state=NORMAL) | |
if chatLog.index('end') != None: | |
lineNumber = float(chatLog.index('end'))-1.0 | |
chatLog.insert(END, userName +": " + entryText) | |
chatLog.tag_add(userName + ": ", lineNumber, lineNumber+((len(userName)+1)/10)) | |
chatLog.tag_config(userName + ": ", foreground="#FF8000", font=("Arial", 12, "bold")) | |
chatLog.config(state=DISABLED) | |
chatLog.yview(END) | |
def loadOtherEntry(chatLog, entryText): | |
if entryText != '': | |
chatLog.config(state=NORMAL) | |
if chatLog.index('end') != None: | |
try: | |
lineNumber = float(chatLog.index('end'))-1.0 | |
except: | |
pass | |
chatLog.insert(END, "Snoo.py Snek: " + entryText+"\n") | |
chatLog.tag_add("Snoo.py Snek: ", lineNumber, lineNumber+0.13) | |
chatLog.tag_config("Snoo.py Snek: ", foreground="#04B404", font=("Arial", 12, "bold")) | |
chatLog.config(state=DISABLED) | |
chatLog.yview(END) | |
base = Tk() | |
base.title('Snoo.py Snek') | |
base.geometry("600x500") | |
base.resizable(width=FALSE, height = FALSE) | |
chatDispl = Text(base, bd = 0, bg = "white", height = "8", width= "50", font = "Arial") | |
chatDispl.insert(END, "Hi, there I'm Snoo.py Snek, What is your name?\n") | |
chatDispl.config(state=DISABLED) | |
scrollBar = Scrollbar(base, command= chatDispl.yview, cursor = "heart") | |
chatDispl['yscrollcommand'] = yscrollcommand=scrollBar.set | |
sendBtn = Button(base, font= 30, text= "Send", width = "12", height = 5, bd = 0, bg="#FFBF00", | |
activebackground= "#FACC2E", command = clickAction) | |
entryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial") | |
entryBox.bind("<Return>", disableEntry) | |
entryBox.bind("<KeyRelease-Return>", pressAction) | |
scrollBar.place(x=576,y=6, height=386) | |
chatDispl.place(x=6,y=6, height=386, width=570) | |
entryBox.place(x=153, y=401, height=90, width=424) | |
sendBtn.place(x=6, y=401, height=90) | |
base.mainloop() |