-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
135 lines (116 loc) · 4.54 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#https://github.com/samuhay/Python-Chat-App/tree/master
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import tkinter
import tkinter as tk
from winsound import *
#Mac Kullanıyorsanız python üzerinden pygame kullanmanız tercih edilir
"""
sudo pip install pygame (Terminal ile yükleyin)
import pygame
pygame.init()
pygame.mixer.init()
sounda= pygame.mixer.Sound("A-Computer Error.wav")
sounda.play()
"""
def receive():
"""Gelen mesajlarla ilgili foksiyon."""
counter=0
while True:
try:
counter+=1
msg = client_socket.recv(BUFSIZ).decode("utf8")
msg_list.insert(tkinter.END, msg)
if counter > 3:
song_id = msg.split(":")[1]
if song_id ==" {ses1}":
PlaySound("A-Computer Error", SND_FILENAME)
if song_id ==" {ses2}":
PlaySound("a-ice-cubes-glass-daniel_simon", SND_FILENAME)
if song_id ==" {ses3}":
PlaySound("Air Plane Ding-Sound", SND_FILENAME)
if song_id ==" {ses4}":
PlaySound("a-service-bell_daniel_simion", SND_FILENAME)
if song_id ==" {ses5}":
PlaySound("A-Tone-His_Self", SND_FILENAME)
except OSError: # Kullanıcının sohbeti terk etme durumu.
break
def send(event=None): # Event binder ile gönderme işlemi.
"""Mesaj gönderme kısımı."""
msg = my_msg.get()
my_msg.set("") # Mesaj gönderdikten sonra inputu temizleme.
client_socket.send(bytes(msg, "utf8"))
if msg == "{quit}":#çıkış komutu sohbetten ayrılma bağlantı kesme
client_socket.close()
top.quit()
def play(id):
if id == 1:
tag = "{ses1}"
client_socket.send(bytes(tag,"utf8"))
return PlaySound("A-Computer Error", SND_FILENAME)
if id == 2:
tag = "{ses2}"
client_socket.send(bytes(tag, "utf8"))
return PlaySound("a-ice-cubes-glass-daniel_simon", SND_FILENAME)
if id == 3:
tag = "{ses3}"
client_socket.send(bytes(tag, "utf8"))
return PlaySound("Air Plane Ding-Sound", SND_FILENAME)
if id == 4:
tag = "{ses4}"
client_socket.send(bytes(tag, "utf8"))
return PlaySound("a-service-bell_daniel_simion", SND_FILENAME)
if id == 5:
tag = "{ses5}"
client_socket.send(bytes(tag, "utf8"))
return PlaySound("A-Tone-His_Self", SND_FILENAME)
def on_closing(event=None):
"""Sohbet ekranı kapanırken çalışır."""
my_msg.set("{quit}")
send()
def create_window():
"""Ses butonunu ekrana eklemek için gerekli kısım"""
window = tk.Toplevel(top)
window.title('Sound')
window.geometry('300x50')
sound1 = tk.Button(window,text="1", command=lambda: play(1)).pack(side=tk.LEFT)
sound2 = tk.Button(window, text="2", command=lambda: play(2)).pack(side=tk.LEFT)
sound3 = tk.Button(window, text="3", command=lambda: play(3)).pack(side=tk.LEFT)
sound4 = tk.Button(window, text="4", command=lambda: play(4)).pack(side=tk.LEFT)
sound5 = tk.Button(window, text="5", command=lambda: play(5)).pack(side=tk.LEFT)
top = tkinter.Tk()
top.geometry("340x500")
top.title("CS 364 APP")
messages_frame = tkinter.Frame(top)
scrollbar = tkinter.Scrollbar(messages_frame) # Mesaj kutusu içinde gezinme için scrollbar.
# Mesaj kutusu ayarları.
msg_list = tkinter.Listbox(messages_frame, height=28, width=51, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
my_msg = tkinter.StringVar()# Gönderilecek mesaj kısımı
my_msg.set("Adınız:")
entry_field = tkinter.Entry(top, textvariable=my_msg, width=50) #Mesaj butonu
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Gönder", command=send)
send_button.pack(side=tk.LEFT)
#sound
sound_button=tk.Button(top,text='Sound',command=create_window)
sound_button.pack(in_=top, side=tk.LEFT)
top.protocol("WM_DELETE_WINDOW", on_closing)
#----Now comes the sockets part----
HOST = input('HOST: ')
PORT = input('PORT: ')
if not PORT: #Port belirtilmezse sabit port
PORT = 1234
else:
PORT = int(PORT)
BUFSIZ = 1024
ADDR = (HOST, PORT)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop()