Environment: Python2.62 + Tkinter. Ubuntu can communicate with each other by entering the IP address and port of the other party. The interface is similar to the IM software design. Because UDP listeners use separate threads, the TCL/TK library may not be supported, and you need to compile them into multiple threads. Windows runs normally and the window size needs to be adjusted. #! /Usr/bin/pythonfromTkinterimport * importsocketimpthreadthreadingim
Environment: Python2.62 + Tkinter, Ubuntu
Enter the IP address and port of the other party to communicate with each other. The interface is similar to the IM software design. Because UDP listeners use separate threads, the TCL/TK library may not be supported, and you need to compile them into multiple threads. Windows runs normally and the window size needs to be adjusted.
#! /Usr/bin/python
From Tkinter import *
Import socket
Import threading
Import tkFont
Class Window:
'''Main window '''
Def _ init _ (self, master ):
Self. fm1 = Frame (master)
Self. fm1.pack ()
# Label (fm1, text = 'local IP Address: '). grid (row = 0, sticky = W, pady = 5)
Label (self. fm1, text = 'target IP & Port: ', font = ft). grid (row = 1, sticky = W, pady = 5)
Label (self. fm1, text = 'message History: ', font = ft). grid (row = 2, stick = W)
Self. ip = StringVar ()
Self. ip. set ('192. 168.1.168 ')
Self. port = StringVar ()
Self. port. set ('123 ')
Self. ipEntry = Entry (self. fm1, width = 15, textvariable = self. ip, font = ft)
Self. ipEntry. grid (row = 1, column = 1, sticky = W)
Self. portEntry = Entry (self. fm1, textvariable = self. port, font = ft)
Self. portEntry. grid (row = 1, column = 2, sticky = W, pady = 5)
Self. fm2 = Frame (master)
Self. fm2.pack ()
Self. msgShow = Text (self. fm2, height = 12, width = 35, font = ft)
Self. scroll = Scrollbar (self. fm2, command = self. msgShow. yview)
Self. msgShow. configure (yscrollcommand = self. scroll. set)
Self. msgShow. pack (side = LEFT, pady = 5)
Self. scroll. pack (side = RIGHT, fill = Y)
Label (master, text = 'Write Your Message Below: ', font = ft). pack ()
Self. msgWrite = Text (master, height = 3, width = 50, font = ft)
Self. msgWrite. pack (pady = 5)
Self. msgWrite. focus_set ()
Self. msgWrite. bind (' ', Self. sendmsg)
Label (root, text = 'Press Ctrl + Enter to Send message', font = ft). pack (side = BOTTOM)
Def sendmsg (self, event = None ):
Self. msg = self. msgWrite. get (1.0, END)
Self. msgShow. insert (END, 'Send: '+ self. msg)
Self. msgShow. see (END)
Self. msgWrite. delete (1.0, END)
Mysocket. sendto (self. msg, (self. ipEntry. get (), int (self. portEntry. get ())))
Return 'Break' # Grate idea!
Class Listenthread (threading. Thread ):
'''Tcl/TK multithreading support is needed '''
Def _ init _ (self ):
Threading. Thread. _ init _ (self)
Def run (self ):
While True:
Data = mysocket. recv (512)
Window. msgShow. insert (END, 'recv: '+ data)
Root = Tk ()
Ft = tkFont. Font (family = 'serif', size = 12, weight = tkFont. NORMAL)
Root. title ('udp test ')
Root. geometry ('270x350 ')
Window = Window (root)
Mysocket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
Mysocket. bind ('', int (window. portEntry. get ())))
Lt = Listenthread ()
Lt. setDaemon (True)
Lt. start ()
Root. mainloop ()
Liuxiao216 original gmail.com content, reprinted please indicate the author, source.