In the development of interface programs with tkinter, it is often necessary to display information on the interface to give users timely feedback and desired results. The Text control allows users to display and edit text with different styles and attributes. It can contain plain text or formatted text, and supports embedded pictures, display hyperlinks, and HTML with CSS formatting.
1: Overall interface
Components used in it:
1, Radiobutton control:, radio button,
2, Text control: text box, display information
3. Scrollbar control: scroll bar
4. Button control: button
Two: overall code framework
def main(self):
lb = Label(root, text='可视化工具', \
#bg='#d3fbfb', \
fg='olive', \
font=('华文新魏', 12), \
width=50, \
height=2, \
relief=FLAT)
lb.place(relx=0.25, rely=0.01)
var = IntVar()
var.set(2)
rd1 = Radiobutton(root, text="func 1", fg='red',variable=var, value=0, command=lambda: self.showResult(var.get()))
rd1.place(relx = 0.23,rely = 0.2)
rd2 = Radiobutton(root, text="func 2", fg='blue',variable=var, value=1,command=lambda: self.showResult(var.get()))
rd2.place(relx = 0.4,rely = 0.2)
rd3 = Radiobutton(root, text="func 3", fg='green',variable=var, value=2,command=lambda: self.showResult(var.get()))
rd3.place(relx = 0.57,rely = 0.2)
rd4 = Radiobutton(root, text="func 4", fg='DeepPink',variable=var, value=3, command=lambda: self.showResult(var.get()))
rd4.place(relx = 0.23,rely = 0.25)
rd5 = Radiobutton(root, text="func 5", fg='SlateBlue',variable=var, value=4,command=lambda: self.showResult(var.get()))
rd5.place(relx = 0.4,rely = 0.25)
rd6 = Radiobutton(root, text="func 6", fg='Brown',variable=var, value=5,command=lambda: self.showResult(var.get()))
rd6.place(relx = 0.57,rely = 0.25)
startButton = Button(root, text='Start',font=('华文新魏', 20), activebackground = "green", command=lambda: self.startFunc(var.get()))
startButton.place(relx=0.35, rely=0.89, relwidth=0.3, relheight=0.1)
Click the start button to display information in the Text control, and the Text control is bound to the scroll bar
def startFunc(self,selectFunc):
self.collectInfo = [1,2,3,4,5,6]
if not self.collectInfo:
return
if self.startButtonFirst == 0:
self.validInfo = Text(root, width=100, height=19)#, undo=True, autoseparators=False
self.validInfo.place(relx=0.01, rely=0.32)
# 放到窗口的右侧, 填充Y竖直方向
self.scroll = Scrollbar()
self.scroll.pack(side=RIGHT,fill=Y)
# 两个控件关联
self.scroll.config(command=self.validInfo.yview)
self.validInfo.config(yscrollcommand=self.scroll.set)
self.validInfo.delete('1.0','end')
if selectFunc == 0:
self.validInfo.insert(INSERT, )
elif selectFunc == 1:
self.validInfo.insert(INSERT, self.collectInfo[1])
elif selectFunc == 2:
self.validInfo.insert(INSERT, self.collectInfo[2])
elif selectFunc == 3:
self.validInfo.insert(INSERT, self.collectInfo[3])
elif selectFunc == 4:
self.validInfo.insert(INSERT, self.collectInfo[4])
elif selectFunc == 5:
self.validInfo.insert(INSERT, self.collectInfo[5])
self.startButtonFirst = 1
Click the radio button to display information
def showResult(self,selectFunc):
if self.startButtonFirst == 0:
return
self.validInfo.delete('1.0','end')
if selectFunc == 0:
self.validInfo.insert(INSERT, self.collectInfo[0])
elif selectFunc == 1:
self.validInfo.insert(INSERT, self.collectInfo[1])
elif selectFunc == 2:
self.validInfo.insert(INSERT, self.collectInfo[2])
elif selectFunc == 3:
self.validInfo.insert(INSERT, self.collectInfo[3])
elif selectFunc == 4:
self.validInfo.insert(INSERT, self.collectInfo[4])
elif selectFunc == 5:
self.validInfo.insert(INSERT, self.collectInfo[5])
Three: Text control
1. Basic attributes
Attributes | illustrate |
---|---|
autoseparator | The default is True, indicating whether to automatically insert a "delimiter" when performing an undo operation (its role is to separate operation records) |
exportselection | The default value is True, which means whether the selected text can be copied to the clipboard, if it is False, it means not allowed. |
insertbackground | Set the color of the insertion cursor, the default is BLACK |
insertborderwidth | Set the border width of the insertion cursor, the default value is 0 |
insertofftime | This option controls the blinking frequency of the cursor (off state) |
insertontime | This option controls the blinking frequency of the cursor (light state) |
selectbackground | Specify the background color of the selected text, the default is determined by the system |
selectborderwidth | Specifies the background color of the selected text, the default value is 0 |
selectforeground | Specify the font color of the selected text, the default value is specified by the system |
setgrid | The default value is False, specify a Boolean value to determine whether to enable grid control |
spacing1 | Specify the blank space between each line and the top of the Text control text block, pay attention to ignore automatic line break, and the default value is 0. |
spacing2 | Specifies the blank interval between lines in the text block of the Text control, ignoring line breaks, the default value is 0 |
spacing3 | Specifies the blank space between each line and the bottom of the text of the Text component, ignoring automatic line wrapping, the default value is 0 |
tabs | Customize the function of the Tab button in the text block described by Tag, which is defined as a width of 8 characters by default. For example, tabs=('1c', '2c', '8c') means that the width of the first 3 Tabs is 1 cm, 2 cm, 8 cm. |
undo | This parameter defaults to False, which means that the "undo" function of the Text control is turned off, and if it is True, it means that it is turned on |
wrap | This parameter is used to set whether to automatically wrap when the length of a line of text exceeds the width set by the width option. The parameter value is none (no automatic line wrap), char (automatically wrap by character), word (automatically wrap by word) |
xscrollcommand | This parameter is associated with the Scrollbar, which means sliding up and down in the horizontal direction |
yscrollcommand | This parameter is associated with the Scrollbar, which means sliding left and right in the vertical direction |
2. Basic method
method | illustrate |
---|---|
bbox(index) | Returns the bounding box of the character at the specified index, the return value is a 4-tuple in the format of (x,y,width,height) |
edit_modified() | This method is used to query and set the modified flag (this flag is used to track whether the content of the Text component has changed) |
edit_redo() | "Redo" the last "undo" operation, if the undo option is set to False, this method is invalid. |
edit_separator() | Insert a "separator" into the stack storing operation records, which is used to indicate that a complete operation has been completed. If the undo option is set to False, this method is invalid. |
get(index1, index2) | Returns the character at a specific position, or a range of literals. |
image_cget(index, option) | Returns the value of the option option embedded in the image object specified by the index parameter, and throws a TclError exception if there is no embedded image object at the given position |
image_create() | Embed an image object at the position specified by the index parameter, and the image object must be a PhotoImage or BitmapImage instance of Tkinter. |
insert(index, text) | Insert a string at the position specified by the index parameter. The first parameter can also be set to INSERT, which means insert at the cursor, and END means insert at the end. |
delete(startindex [, endindex]) | Delete characters at a specific position, or a range of text. |
see(index) | Returns True if the text at the specified index is visible, otherwise returns False. |
Four: color summary supported by tkinter
There are many color settings in tkinter, here is a summary, no need to look around
'''#FFB6C1 LightPink light pink
#FFC0CB Pink Pink
#DC143C Crimson Crimson/Scarlet
#FFF0F5 LavenderBlush lavender
#DB7093 PaleVioletRed weak violet red
#FF69B4 HotPink passionate pink
#FF1493 DeepPink deep pink
#C71585 MediumVioletRed Medium Violet Red
#DA70D6 Orchid dark purple/orchid purple
#D8BFD8 Thistle Blue
#DDA0DD Plum Plum/Plum Purple
#EE82EE Violet Violet
#FF00FF Magenta Magenta/Rose
#FF00FF Fuchsia Fuchsia / Lantern Begonia
#8B008B DarkMagenta deep magenta
#800080 Purple Purple
#BA55D3 MediumOrchid Medium Orchid Violet
#9400D3 DarkViolet dark violet
#9932CC DarkOrchid dark orchid purple
#4B0082 Indigo Indigo/Violet
#8A2BE2 BlueViolet blue violet
#9370DB MediumPurple
#7B68EE MediumSlateBlue medium dark blue/medium slate blue
#6A5ACD SlateBlue
#483D8B DarkSlateBlue dark gray blue / dark slate blue
#E6E6FA Lavender Lavender / Lavender Lavender
#F8F8FF GhostWhite Ghost White
#0000FF Blue pure blue
#0000CD MediumBlue medium blue
#191970 MidnightBlue midnight blue
#00008B DarkBlue
#000080 Navy navy blue
#4169E1 RoyalBlue Royal Blue / Royal Blue
#6495ED CornflowerBlue cornflower blue
#B0C4DE LightSteelBlue Bright steel blue
#778899 LightSlateGray Bright blue gray / bright slate gray
#708090 SlateGray gray stone / slate gray
#1E90FF DodgerBlue flash blue / Dodge blue
#F0F8FF AliceBlue Alice Blue
#4682B4 SteelBlue steel blue / iron blue
#87CEFA LightSkyBlue bright sky blue
#87CEEB SkyBlue sky blue
#00BFFF DeepSkyBlue deep sky blue
#ADD8E6 LightBlue Bright Blue
#B0E0E6 PowderBlue powder blue/gunpowder green
#5F9EA0 CadetBlue Military Blue/Military Blue
#F0FFFF Azure Azure
#E0FFFF LightCyan light cyan
#AFEEEE PaleTurquoise Weak Green Jewel
#00FFFF Cyan Blue
#00FFFF Aqua Light Green/Aqua
#00CED1 DarkTurquoise Dark Emerald
#2F4F4F DarkSlateGray Dark Slate Gray/Dark Slate Gray
#008B8B DarkCyan Dark blue
#008080 Teal teal color
#48D1CC MediumTurquoise Medium Turquoise
#20B2AA LightSeaGreen light ocean green
#40E0D0 Turquoise Emerald
#7FFFD4 Aquamarine Gemstone Green
#66CDAA MediumAquamarine Medium gem green
#00FA9A MediumSpringGreen Medium Spring Green
#F5FFFA MintCream Peppermint Cream
#00FF7F SpringGreen
#3CB371 MediumSeaGreen Medium sea green
#2E8B57 SeaGreen ocean green
#F0FFF0 Honeydew Honey/melon
#90EE90 LightGreen light green
#98FB98 PaleGreen Light green color
#8FBC8F DarkSeaGreen dark ocean green
#32CD32 LimeGreen Flash dark green
#00FF00 Lime flash green
#228B22 ForestGreen forest green
#008000 Green pure green
#006400 DarkGreen Dark green
#7FFF00 Chartreuse Yellow Green/Charter Wine Green
#7CFC00 LawnGreen grass green / lawn green
#ADFF2F GreenYellow
#556B2F DarkOliveGreen dark olive green
#9ACD32 YellowGreen Yellow Green
#6B8E23 OliveDrab olive brown
#F5F5DC Beige beige/taupe
#FAFAD2 LightGoldenrodYellow bright chrysanthemum yellow
#FFFFF0 Ivory Ivory
#FFFFE0 LightYellow light yellow
#FFFF00 Yellow pure yellow
#808000 Olive Olive
#BDB76B DarkKhaki dark khaki/dark khaki
#FFFACD LemonChiffon Lemon Silk
#EEE8AA PaleGoldenrod gray chrysanthemum/blue unicorn color
#F0E68C Khaki Tan/khaki cloth
#FFD700 Gold gold
#FFF8DC Cornsilk
#DAA520 Goldenrod golden chrysanthemum yellow
#B8860B DarkGoldenrod Dark golden chrysanthemum yellow
#FFFAF0 FloralWhite Floral White
#FDF5E6 OldLace old color / old lace
#F5DEB3 Wheat light yellow/wheat color
#FFE4B5 Moccasin Buckskin/Moccasin Boots
#FFA500 Orange Orange
#FFEFD5 PapayaWhip Papaya/Papaya
#FFEBCD BlanchedAlmond white apricot color
#FFDEAD NavajoWhite Navajo White/Indigenous White
#FAEBD7 AntiqueWhite Antique White
#D2B48C Tan Brown
#DEB887 BurlyWood hardwood color
#FFE4C4 Bisque clay yellow
#FF8C00 DarkOrange dark orange
#FAF0E6 Linen Linen
#CD853F Peru Peruvian color
#FFDB9 PeachPuff pink flesh color
#F4A460 SandyBrown
#D2691E Chocolate chocolate color
#8B4513 SaddleBrown heavy brown / saddle brown
#FFF5EE Seashell Seashell
#A0522D Sienna Yellow Ocher
#FFA07A LightSalmon light salmon flesh color
#FF7F50 Coral Coral
#FF4500 OrangeRed orange red
#E9967A DarkSalmon dark meat/salmon color
#FF6347 Tomato tomato red
#FFE4E1 MistyRose light rose/mist rose
#FA8072 Salmon fresh meat/salmon color
#FFFAFA Snow snow white
#F08080 LightCoral light coral color
#BC8F8F RosyBrown rose brown
#CD5C5C IndianRed Indian Red
#FF0000 Red pure red
#A52A2A Brown Brown
#B22222 FireBrick firebrick/firebrick
#8B0000 DarkRed deep red
#800000 Maroon Maroon
#FFFFFF White pure white
#F5F5F5 WhiteSmoke White Smoke
#DCDCDC Gainsboro light gray
#D3D3D3 LightGrey light gray
#C0C0C0 Silver silver gray
#A9A9A9 DarkGray dark gray
#808080 Gray gray
#696969 DimGray Dim gray
#000000 Black pure black'''