Python tkinter layout: simple implementation of chat window, pythontkinter
This example shows a simple chat window with python tkinter layout. 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.
How does python tkinter set the position of a component in a window? For example, if a button is to be on the left of the window .........
This is related to the encoding method,
Add #-*-coding: utf8 -*-
Chinese is displayed.
Self. Button (self. trspt_frm ,\
Text = 'send file ',\
Command = self. send_file ,\
). Pack (side = Tkinter. LEFT, fill = Tkinter. BOTH)
You can set the button position by setting parameters in pack.
How can I add a title to the window and change the button text to Python Tkinter?
From Tkinter import *
Root = Tk ()
Strvar = StringVar ()
Strvar. set ("Please Click Me .")
Def handler ():
Strvar. set ("You Have Clicked Me .")
Btn = Button (root, textvariable = strvar, command = handler)
Btn. pack ()
Root. mainloop ()
From Tkinter import *
Root = Tk ()
Root. title ("Hello, World! ")
Root. mainloop ()
I also replied in the post bar. Reading books is also attractive.