This example shows a simple chat window for a Python tkinter layout. Share it for everyone's reference. Here's how:
This example shows a simple chat window, can be implemented below the input chat content, click Send, can be added to the above chat record list. Now it's just a "stand-alone" version.
There is a vacancy on the right to put something else. Interested readers can further make the sockets to chat with each other.
Here is the function code section:
From Tkinter import *import datetimeimport timeroot = Tk () root.title (Unicode (' with xxx chat ', ' eucgb2312_cn ') #发送按钮事件def SendMessage (): #在聊天内容上方加一行 show sender and send 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 ', EN D) text_msg.delete (' 0.0 ', END) #创建几个frame作为容器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 ') # #创建需要的几个元素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) #创建一个绿色的tagtext _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) #把元素填充进frametext_ Msglist.grid () Text_msg.grid () Button_sendmsg.grid (sticky=e) #主事件循环root. Mainloop ()
Here's how to run:
Hopefully this article will help you with Python programming.