此代码用于聊天程序,帮助我在不同的笔记本电脑上运行

时间:2018-05-29 19:36:40

标签: python-3.x

#server( the server page and client page should be put in different python pages)
from tkinter import*
import socket #imports the socket library 
import sys
import time
import threading # used to set a timer for recieveing  
window1=Tk()
window1.title("Server page")
window1.geometry('800x800')

background_image=PhotoImage(file="h123.png") #the background picture
background_label =Label(window1, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


input_box=Entry(window1, width=90) #masseage text input value
input_box.place(x=19,y=650)

shift_value=Entry(window1,width=5) #shift input value
shift_value.place(x=680,y=700)
shift_lapl=Label(text="Enter The Encryption key",bg='black',fg='white')#shift label
shift_lapl.place(x=620,y=730)

def myfunction(event):#the function for the scroll bar
    canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200)

def encryption(): #The function for encrypting the chat and called when clicked the Encrypt button
    d=input_box.get()
    w=shift_value.get()
    global shift
    global result
    global phrase
    global o
    o=''
    phrase =d
    shift=int(w)
    p=[]
    for character in phrase:
        x = ord(character)

        x = x + shift

        p.append(chr (x))
    for y in p:
        o+=y  
    shift_value.delete(0,100)    
    input_box.delete(0,100)
    input_box.insert(0,(o))

def word(): #Th decryption function used when clicking the Decrypt button 
    global label
    g=label.cget("text")
    f=len(g)
    d=g[11:f]
    l1=[]
    r=''
    for i in d:
        z=ord(i)
        w=z-(int(shift_value.get()))#the value put in the shift box
        l1.append(chr(w))
    for t in l1:
        r+=t
    label=Label(frame,text='Unencrupted: '+r,fg='green')
    label.pack(side=TOP)         
    return 'break'        


def serverc():#servre creation
        global s
        global q
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#TCP connection for sending words in an orderded way
        host = '192.168.56.1'
        print("server will start on host:",host)
        port=8080
        addr=(host,port)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)#to allow us reuse the port once again
        s.bind(addr)  #binding to the host and the port written 
        s.listen(10)
        conn,addr=s.accept()
        q=conn

def server_send():#Sending massages and showing it in the chat box(chat_box) 
            v=input_box.get()
            global label
            label = Label(frame, text='This PC: '+v,fg='blue')
            label.pack(side=TOP)#showing the text in the frame in labels
            input_box.delete(0,100)
            message =v
            message = message.encode()
            q.send(message)
            print(message)
            return 'break'
def server_recieve():#recieving massages
    threading.Timer(5.0,server_recieve).start()
    try:
        global label
        incoming_message = q.recv(1024)# the maximum number of bytes of wors that a socket can send or recieve
        incoming_message = incoming_message.decode()
        label = Label(frame, text='Client PC: '+incoming_message,fg='green')
        label.pack(side=TOP)
        return 'break'
    except:
        print(1)
b1=Button(window1,width=5,height=2,text='send',bg='gray',fg='black', command=server_send)#send button
b1.place(x=675,y=640)

B=Button(window1,text="Log in",width=10,height=2 ,bg='white', fg='black', command=serverc)#log in button  
B.place(x=20,y=10)


b2=Button(window1,width=10,height=2,text="Encrypt",fg="white",bg="red", command=encryption)#incrept button
b2.place(x=450,y=700)

b3=Button(window1,width=10,height=2,text="Decrypt",fg="white",bg="blue", command=word)#un incrept button
b3.place(x=140,y=700)


#the new edits

frame = Frame(window1, width=800, height=800)#the chat frame that shows the massages sent and recieved
input_box.bind("<Return>", server_send)
frame.pack()

canvas=Canvas(frame)
framen=Frame(canvas)
myscrollbar=Scrollbar(frame,orient="vertical",command=canvas.xview)
canvas.configure(yscrollcommand=myscrollbar.set)

myscrollbar.pack(side="left",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=framen,anchor='nw')
framen.bind("<Configure>",myfunction)
myfunction(server_send)

server_recieve()

window1.mainloop()

#the client page  

from tkinter import*
import socket
import sys
import time
import threading
global shift
global result
global phrase
global o
window1=Tk()
window1.title("client page")
window1.geometry('800x800')

background_image=PhotoImage(file="h123.png")
background_label =Label(window1, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

input_box=Entry(window1, width=90) #masseage text
input_box.place(x=19,y=650)

shift_value=Entry(window1,width=5) #shift value
shift_value.place(x=680,y=700)
shift_lapl=Label(text="Enter The Encryption value")
shift_lapl.place(x=620,y=730)

def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=200)

def encryption():
    d=input_box.get()
    w=shift_value.get()

    o=''
    phrase =d
    shift=int(w)
    p=[]
    for character in phrase:
        x = ord(character)

        x = x + shift

        p.append(chr (x))
    for y in p:
        o+=y  
    shift_value.delete(0,100)    
    input_box.delete(0,100)
    input_box.insert(0,(o))

def word():
    global label
    g=label.cget("text")
    f=len(g)
    d=g[11:f]
    l1=[]
    r=''
    for i in d:
        z=ord(i)
        w=z-(int(shift_value.get()))
        l1.append(chr(w))
    for t in l1:
        r+=t
    label=Label(frame,text='Unencrupted: '+r,fg='blue')
    label.pack(side=TOP)         
    return 'break'

def clientc():#client creation
    global s
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    host ='192.168.56.1'
    port = 8080
    s.connect((host,port))
    print(" Connected to chat server")
import time
def client_recieve():
    threading.Timer(5.0,client_recieve).start()
    try:
        global label
        incoming_message = s.recv(1024)
        incoming_message = incoming_message.decode()
        label=Label(frame,text=('Server PC: '+incoming_message),fg='blue')
        label.pack(side=TOP)
        return 'break'
    except:
        print(1)
def client_send():
        v=input_box.get()
        global label
        label = Label(frame, text='This PC: '+v,fg='green')
        label.pack(side=TOP)        
        input_box.delete(0,100)                
        message = v
        message = message.encode()
        s.send(message)        
        return 'break'                  
b1=Button(window1,width=5,height=2,text='send',bg='gray',fg='black', command=client_send)#send button
b1.place(x=675,y=640)

B=Button(window1,width=10,height=2,text="Log in", bg='white', fg='black', command=clientc)#log in button  
B.place(x=20,y=10)


b2=Button(window1,width=10,height=2,text="Encrypt",fg="white",bg="red", command=encryption)#Encrypt button
b2.place(x=450,y=700)

b3=Button(window1,width=10,height=2,text="Unencrypt",fg="white",bg="blue", command=word)# unencrypt button
b3.place(x=140,y=700)

# the two lapls at the top of screan for messages

#the new edits

frame = Frame(window1, width=300, height=300)
input_box.bind("<Return>", client_send)
frame.pack()

canvas=Canvas(frame)
framen=Frame(canvas)
myscrollbar=Scrollbar(frame,orient="vertical",command=canvas.xview)
canvas.configure(yscrollcommand=myscrollbar.set)

myscrollbar.pack(side="left",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=framen,anchor='nw')
framen.bind("<Configure>",myfunction)
myfunction(client_send)



client_recieve()  
window1.mainloop()

该软件在同一台笔记本电脑上运行服务器页面和客户端页面时可以正常工作

1-当您打开它时,首先单击服务器页面中的登录,然后单击客户端页面中的登录 通常2开始聊天 3-当您尝试在笔记本电脑上运行客户端代码而另一台服务器上运行该程序时,该程序不起作用

1 个答案:

答案 0 :(得分:-1)

您需要将程序转换为.exe

一些stackoverflow帖子已经记录了该过程:

How can I convert a .py to .exe for Python?

Process to convert simple Python script into Windows executable