This example shows a simple chat window for Python's tkinter layout. Share for everyone to use for reference. The specific methods are as follows:
This example shows a simple chat window, can be implemented below the input chat content, click Send, can be added to the top of the chat record list. Now it's just a "stand-alone" version.
There is an empty space on the right to put something else on. Interested readers can further make the socket to chat with each other.
Here is the functional Code section:
From tkinter Import * Import datetime import Time root = Tk () root.title (Unicode (' Chat with xxx ', ' eucgb2312_cn ') #发送按钮事件 def s Endmessage (): #在聊天内容上方加一行 show sender and send time msgcontent = Unicode (' I: ', ' eucgb2312_cn ') + time.strftime ("%y-%m-%d%h:%m:%s", Tim E.localtime ()) + ' \ n ' text_msglist.insert (end, msgcontent, ' green ') text_msglist.insert (end, Text_msg.get (' 0.0 ', end) Text_msg.delete (' 0.0 ', end) #创建几个frame作为容器 the Frame_left_top = FRAME (width=380, height=270, bg= ' white ') Frame_left_cen ter = FRAME (width=380, height=100, bg= ' white ') Frame_left_bottom = FRAME (width=380, height=20) Frame_right = FRAME (w idth=170, height=400, bg= ' White ') # #创建需要的几个元素 text_msglist = text (frame_left_top) text_msg = text (frame_left_cente
R); Button_sendmsg = Button (Frame_left_bottom, Text=unicode (' Send ', ' eucgb2312_cn '), command=sendmessage) #创建一个绿色的tag Text_ Msglist.tag_config (' green ', foreground= ' #008B00 ') #使用grid设置各个容器位置 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) frame_left_bottom.grid_propagate (0
) #把元素填充进frame Text_msglist.grid () Text_msg.grid () Button_sendmsg.grid (sticky=e) #主事件循环 Root.mainloop ()
Here's a screenshot of the run:
I hope this article will help you with your Python programming.