Abdullahafzalkhan

python chat app - client

Apr 26th, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. import socket, threading
  2. from tkinter import *
  3. from tkinter import scrolledtext
  4. from keyboard import add_hotkey
  5. connected = False
  6. conected2 = False
  7. firstTime = True
  8. firstTimeS = True
  9. f = True
  10. format = 'utf-8'
  11. port = 8080
  12. server = socket.gethostbyname(socket.gethostname())
  13. addr = (server,port)
  14. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  15. root = Tk(className="- Pychatter")
  16. root.geometry("400x400")
  17. root.configure(bg='blue')
  18. messageVar = StringVar(value="")
  19. usernameVar = StringVar(value="")
  20. def firstConnect():
  21.     print("firs")
  22.     if firstTime:
  23.         print("firstTime")
  24.  
  25. def connect():
  26.     global connected
  27.     if not connected:
  28.         output.insert(INSERT, "\n[INFO] Connecting to server...")
  29.     try:
  30.         client.connect(addr)
  31.         connected = True
  32.         output.insert(INSERT, "\n[SUCCESS] Connected to server")
  33.     except ConnectionRefusedError:
  34.         output.insert(INSERT, "\n[ERROR]Unable to connect to server")
  35.     except OSError:
  36.         output.insert(INSERT, "\n[ERROR] Already connected or server is down")
  37.        
  38. def submit():
  39.     global firstTime
  40.     global firstTimeS
  41.     if not firstTime:
  42.         try:
  43.             if firstTimeS:
  44.                 client.send(f"FirstTime,{str(usernameVar.get()).capitalize()}".encode(format))
  45.                 firstTimeS = False
  46.                 print("f time")
  47.                 messageVar.set(value="")
  48.             else:
  49.                 msg = usernameVar.get().capitalize() + ": "+ messageVar.get()
  50.                 client.send(msg.encode('utf-8'))
  51.                 print("send")
  52.                 messageVar.set(value="")
  53.         except:
  54.             output.insert(INSERT, '\n[ERROR] NOT CONNECTED TO SERVER')
  55.     else:
  56.         usernameVar.set(messageVar.get())
  57.         output.insert(INSERT,"\nWelcome, %s" %messageVar.get())
  58.         messageVar.set(value="")
  59.         firstTime = False
  60.  
  61. def check():
  62.     print("started")
  63.     global connected
  64.     while True:
  65.         try:
  66.             if connected:
  67.                 smsg = client.recv(1024)
  68.                 print("msg gotten")
  69.                 output.insert(INSERT, "\n"+ smsg.decode('utf-8'))
  70.         except:
  71.             pass
  72.  
  73. canvas = Canvas(root, width = 400, height = 400, bg="black")
  74. output = scrolledtext.ScrolledText(root, wrap = WORD, bg='black', fg="lime")
  75. output.config(width = 40, height = 15, font = (12))
  76. output.insert(INSERT, 'Welcome to the cave')
  77. message = Entry(root, font = (13),textvariable=messageVar,width = 13, bg = 'black', fg = "lime")
  78. submitButton = Button(root, text="Submit", command = submit, font = (13), bg = "black", fg = 'lime')
  79. connectButton = Button(root, text="Connect to server", command=connect, font = (13), bg="black", fg="lime", activebackground="black", activeforeground ='lime')
  80. clearButton = Button(root, text="Clear", command=lambda: output.delete('1.0', END), font = (12), bg="black", fg="lime", activebackground="black", activeforeground ='lime')
  81. canvas.create_window(300, 330, window = connectButton)
  82. canvas.create_window(200, 330, window = submitButton)
  83. canvas.create_window(150,330, window = clearButton)
  84. canvas.create_window(200, 140, window = output)
  85. canvas.create_window(200, 300, window = message)
  86. add_hotkey('enter', submit)
  87.  
  88. if __name__ == '__main__':
  89.     output.insert(INSERT, "\nEnter Display Name")
  90.     checkThread = threading.Thread(target=check,daemon=True)
  91.     checkThread.start()
  92.     firstConnectThread = threading.Thread(target=firstConnect, daemon = True)
  93.     firstConnectThread.start()
  94.     canvas.pack(fill=BOTH, expand=True)  
  95.     root.tk.call("tk", "scaling", 2.0)
  96.     root.mainloop()
  97.  
  98.  
  99.    
  100.  
Add Comment
Please, Sign In to add comment