如何编译python聊天程序到exe?

时间:2016-09-19 18:10:36

标签: python-2.7 chat

这是一个双向聊天室“主持人”的代码。客户端的代码非常相似。我试图用各种方法将它们编译成一个exe,但没有一个得到很好的结果。 exe只是不运行。我得到了所有模块,我尝试了很多代码(许多代码也加入了dll文件等。)我尝试将其编译为pyw,因为它里面有图形。还有一个py文件有一些更多的信息,这两个(客户端和主机)pyw文件以某种方式得到,但他们仍然可以运行python.exe没有它。我不知道如何帮助您理解,如果您有任何疑问请告诉我。提前谢谢。

这是python 2.7代码中的主机:

import thread
from ChatFns import *



#---------------------------------------------------#
#---------INITIALIZE CONNECTION VARIABLES-----------#
#---------------------------------------------------#
#Initiate socket and bind port to host PC
WindowTitle = 'Chat - Host User'
s = socket(AF_INET, SOCK_STREAM)
HOST = gethostname()
PORT = 8000
conn = ''
s.bind((HOST, PORT))



#---------------------------------------------------#
#------------------ MOUSE EVENTS -------------------#
#---------------------------------------------------#
def ClickAction():
    #Write message to chat window
    EntryText = FilteredMessage(EntryBox.get("0.0",END))
    LoadMyEntry(ChatLog, EntryText)

    #Scroll to the bottom of chat windows
    ChatLog.yview(END)

    #Erace previous message in Entry Box
    EntryBox.delete("0.0",END)

    #Send my mesage to all others
    conn.sendall(EntryText)





#---------------------------------------------------#
#----------------- KEYBOARD EVENTS -----------------#
#---------------------------------------------------#
def PressAction(event):
    EntryBox.config(state=NORMAL)
    ClickAction()
def DisableEntry(event):
    EntryBox.config(state=DISABLED)




#---------------------------------------------------#
#-----------------GRAPHICS MANAGEMENT---------------#
#---------------------------------------------------#

#Create a window
base = Tk()
base.title(WindowTitle)
base.geometry("400x470")
base.resizable(width=FALSE, height=FALSE)

#Create a Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial",)
ChatLog.insert(END, "Waiting for client user to connect...\n")
ChatLog.config(state=DISABLED)

#Bind a scrollbar to the Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set

#Create the Button to send message
SendButton = Button(base, font=30, text="Send", width="12", height=5,
                    bd=0, bg="#E6E6E6", activebackground="#FA5858",
                    command=ClickAction)

#Create the box to enter message
EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")
EntryBox.bind("<Return>", DisableEntry)
EntryBox.bind("<KeyRelease-Return>", PressAction)

#Place all components on the screen
scrollbar.place(x=376,y=6, height=386)
ChatLog.place(x=6,y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=60, width=265)
SendButton.place(x=6, y=401, height=60)



#---------------------------------------------------#
#----------------CONNECTION MANAGEMENT--------------#
#---------------------------------------------------#
def GetConnected():
    s.listen(1)
    global conn
    conn, addr = s.accept()
    LoadConnectionInfo(ChatLog, 'Connected with: ' + str(addr) + '\n---------------------------------------------------------------')

    while 1:
        try:
            data = conn.recv(1024)
            LoadOtherEntry(ChatLog, data)
            if base.focus_get() == None:
                FlashMyWindow(WindowTitle)
                playsound('notif.wav')
        except:
            LoadConnectionInfo(ChatLog, '\n [ User disconnected. ]\n [ Waiting for them to connect...] \n  ')
            GetConnected()

    conn.close()

thread.start_new_thread(GetConnected,())

base.mainloop()

1 个答案:

答案 0 :(得分:0)

Python是一种解释型语言,它不应该被编译成.exe文件。但是,像 http://py2exe.org/ 这样的工具可以用Python创建一个可执行文件,将解释器本身绑定到文件中。