'How to use both tkinter and socket in python
By my project, I got into problem. I made code without tkinter with socket. It worked really well, then I added GUI to my code, and it shows me I used socket wrong. Here's my code with GUI.
import socket as s
from tkinter import *
from tkinter import simpledialog as d
t=Tk()
con=Text(t,font='Arail 12',state='disabled')
srcoll=Scrollbar(t,command=con.yview)
srcoll.pack(fill="y", side="right", anchor="e")
con.config(yscrollcommand = srcoll.set)
con.pack(expand=True,fill='both')
bb=Frame(t)
Joined=False
def recv():
In=c.recv(2048)
print(In.decode('utf-8'))
t.after(50,recv)
def send():
if messbar.get() != '' and Joined:
ID=open('.\\Name.txt',encoding='utf-8').read()
Out=ID+messbar.get()
Out=Out.encode('utf-8')
print(ID+' >'+messbar.get())
messbar.delete('0',END)
c.send(Out)
messbar=Entry(bb)
Button(bb,text='Send',command=lambda:send()).pack(side='right')
messbar.pack(side='right',fill='x',anchor='e')
messbar.bind('<Return>',lambda a:send())
bb.pack(fill='x',anchor='s')
t.title('△△△')
def print(text):
con['state'] = 'normal'
con.insert(END,text+'\n')
con['state'] = 'disabled'
c=s.socket(s.AF_INET,s.SOCK_STREAM)
IPPort=d.askinteger('Port','Set your chat\'s port!')
print('Opened Server with Port: '+str(IPPort)+' IP: '+s.gethostbyname(s.gethostname()))
c.listen(5)
c,add=c.accept()
print(add,'joined.')
Joined=True
t.after(50,recv)
t.mainloop()
And this is what I've got for error.
Traceback (most recent call last):
File "D:\△△△\△△△", line 38, in <module>
c.listen(5)
OSError: [WinError 10022] An invalid argument was supplied
I have no idea why c.listen(5) cause OSError.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
