This article mainly introduces python's tkinter layout and Simple Chat Window implementation method. It gives a preliminary introduction to tkinter usage and application demonstration, for more information about how to use tkinter in python, see the following example. Share it with you for your reference. The specific method is as follows:
This instance shows a simple chat window. You can enter the chat content below and click send to add it to the chat record list above. Now it is only the "standalone" version.
You can leave some other items on the right. Interested readers can further chat with each other through socket.
The following is the function code:
From Tkinter import * import datetimeimport timeroot = Tk () root. title (unicode ('chatting with xxx ', 'eucgb2312 _ cn') # Send button event def sendmessage (): # add a line above the chat content to display the sender and sending time msgcontent = unicode ('I:', 'eucgb2312 _ cn') + time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime () + '\ n' text_msglist.insert (END, msgcontent, 'green') text_msglist.insert (END, text_msg.get ('0. 0', END) text_msg.delete ('0. 0', END) # create several frames as the container frame_left_top = frame (width = 380, height = 270, bg = 'white') frame_left_center = Frame (width = 380, height = 100, bg = 'white') frame_left_bottom = Frame (width = 380, height = 20) frame_right = Frame (width = 170, height = 400, bg = 'white ') # create the required elements text_msglist = Text (frame_left_top) text_msg = Text (frame_left_center); button_sendmsg = Button (frame_left_bottom, text = unicode ('send ', 'eucgb2312 _ cn'), command = sendmessage) # create a green tagtext_msglist.tag_config ('green', foreground = '#008b00 ') # Use grid to set frame_left_top.grid (row = 0, column = 0, padx = 2, pady = 5) frame_left_center.grid (row = 1, column = 0, padx = 2, pady = 5) frame_left_bottom.grid (row = 2, column = 0) frame_right.grid (row = 0, column = 1, rowspan = 3, padx = 4, pady = 5) frame_left_top.grid_propagate (0) frame_left_center.grid_propagate (0) padding (0) # Fill the element in frametext_msglist.grid () text_msg.grid () button_sendmsg.grid (sticky = E) # Main Event loop root. mainloop ()
Run the following command:
I hope this article will help you with Python programming.