Python tkinter Layout-Simple Chat Window implementation

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.