Python3-based tkinter Text text box plus scroll bar to display information

        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

  1. '''#FFB6C1 LightPink light pink

  2. #FFC0CB Pink Pink

  3. #DC143C Crimson Crimson/Scarlet

  4. #FFF0F5 LavenderBlush lavender

  5. #DB7093 PaleVioletRed weak violet red

  6. #FF69B4 HotPink passionate pink

  7. #FF1493 DeepPink deep pink

  8. #C71585 MediumVioletRed Medium Violet Red

  9. #DA70D6 Orchid dark purple/orchid purple

  10. #D8BFD8 Thistle Blue

  11. #DDA0DD Plum Plum/Plum Purple

  12. #EE82EE Violet Violet

  13. #FF00FF Magenta Magenta/Rose

  14. #FF00FF Fuchsia Fuchsia / Lantern Begonia

  15. #8B008B DarkMagenta deep magenta

  16. #800080 Purple Purple

  17. #BA55D3 MediumOrchid Medium Orchid Violet

  18. #9400D3 DarkViolet dark violet

  19. #9932CC DarkOrchid dark orchid purple

  20. #4B0082 Indigo Indigo/Violet

  21. #8A2BE2 BlueViolet blue violet

  22. #9370DB MediumPurple

  23. #7B68EE MediumSlateBlue medium dark blue/medium slate blue

  24. #6A5ACD SlateBlue

  25. #483D8B DarkSlateBlue dark gray blue / dark slate blue

  26. #E6E6FA Lavender Lavender / Lavender Lavender

  27. #F8F8FF GhostWhite Ghost White

  28. #0000FF Blue pure blue

  29. #0000CD MediumBlue medium blue

  30. #191970 MidnightBlue midnight blue

  31. #00008B DarkBlue

  32. #000080 Navy navy blue

  33. #4169E1 RoyalBlue Royal Blue / Royal Blue

  34. #6495ED CornflowerBlue cornflower blue

  35. #B0C4DE LightSteelBlue Bright steel blue

  36. #778899 LightSlateGray Bright blue gray / bright slate gray

  37. #708090 SlateGray gray stone / slate gray

  38. #1E90FF DodgerBlue flash blue / Dodge blue

  39. #F0F8FF AliceBlue Alice Blue

  40. #4682B4 SteelBlue steel blue / iron blue

  41. #87CEFA LightSkyBlue bright sky blue

  42. #87CEEB SkyBlue sky blue

  43. #00BFFF DeepSkyBlue deep sky blue

  44. #ADD8E6 LightBlue Bright Blue

  45. #B0E0E6 PowderBlue powder blue/gunpowder green

  46. #5F9EA0 CadetBlue Military Blue/Military Blue

  47. #F0FFFF Azure Azure

  48. #E0FFFF LightCyan light cyan

  49. #AFEEEE PaleTurquoise Weak Green Jewel

  50. #00FFFF Cyan Blue

  51. #00FFFF Aqua Light Green/Aqua

  52. #00CED1 DarkTurquoise Dark Emerald

  53. #2F4F4F DarkSlateGray Dark Slate Gray/Dark Slate Gray

  54. #008B8B DarkCyan Dark blue

  55. #008080 Teal teal color

  56. #48D1CC MediumTurquoise Medium Turquoise

  57. #20B2AA LightSeaGreen light ocean green

  58. #40E0D0 Turquoise Emerald

  59. #7FFFD4 Aquamarine Gemstone Green

  60. #66CDAA MediumAquamarine Medium gem green

  61. #00FA9A MediumSpringGreen Medium Spring Green

  62. #F5FFFA MintCream Peppermint Cream

  63. #00FF7F SpringGreen

  64. #3CB371 MediumSeaGreen Medium sea green

  65. #2E8B57 SeaGreen ocean green

  66. #F0FFF0 Honeydew Honey/melon

  67. #90EE90 LightGreen light green

  68. #98FB98 PaleGreen Light green color

  69. #8FBC8F DarkSeaGreen dark ocean green

  70. #32CD32 LimeGreen Flash dark green

  71. #00FF00 Lime flash green

  72. #228B22 ForestGreen forest green

  73. #008000 Green pure green

  74. #006400 DarkGreen Dark green

  75. #7FFF00 Chartreuse Yellow Green/Charter Wine Green

  76. #7CFC00 LawnGreen grass green / lawn green

  77. #ADFF2F GreenYellow

  78. #556B2F DarkOliveGreen dark olive green

  79. #9ACD32 YellowGreen Yellow Green

  80. #6B8E23 OliveDrab olive brown

  81. #F5F5DC Beige beige/taupe

  82. #FAFAD2 LightGoldenrodYellow bright chrysanthemum yellow

  83. #FFFFF0 Ivory Ivory

  84. #FFFFE0 LightYellow light yellow

  85. #FFFF00 Yellow pure yellow

  86. #808000 Olive Olive

  87. #BDB76B DarkKhaki dark khaki/dark khaki

  88. #FFFACD LemonChiffon Lemon Silk

  89. #EEE8AA PaleGoldenrod gray chrysanthemum/blue unicorn color

  90. #F0E68C Khaki Tan/khaki cloth

  91. #FFD700 Gold gold

  92. #FFF8DC Cornsilk

  93. #DAA520 Goldenrod golden chrysanthemum yellow

  94. #B8860B DarkGoldenrod Dark golden chrysanthemum yellow

  95. #FFFAF0 FloralWhite Floral White

  96. #FDF5E6 OldLace old color / old lace

  97. #F5DEB3 Wheat light yellow/wheat color

  98. #FFE4B5 Moccasin Buckskin/Moccasin Boots

  99. #FFA500 Orange Orange

  100. #FFEFD5 PapayaWhip Papaya/Papaya

  101. #FFEBCD BlanchedAlmond white apricot color

  102. #FFDEAD NavajoWhite Navajo White/Indigenous White

  103. #FAEBD7 AntiqueWhite Antique White

  104. #D2B48C Tan Brown

  105. #DEB887 BurlyWood hardwood color

  106. #FFE4C4 Bisque clay yellow

  107. #FF8C00 DarkOrange dark orange

  108. #FAF0E6 Linen Linen

  109. #CD853F Peru Peruvian color

  110. #FFDB9 PeachPuff pink flesh color

  111. #F4A460 SandyBrown

  112. #D2691E Chocolate chocolate color

  113. #8B4513 SaddleBrown heavy brown / saddle brown

  114. #FFF5EE Seashell Seashell

  115. #A0522D Sienna Yellow Ocher

  116. #FFA07A LightSalmon light salmon flesh color

  117. #FF7F50 Coral Coral

  118. #FF4500 OrangeRed orange red

  119. #E9967A DarkSalmon dark meat/salmon color

  120. #FF6347 Tomato tomato red

  121. #FFE4E1 MistyRose light rose/mist rose

  122. #FA8072 Salmon fresh meat/salmon color

  123. #FFFAFA Snow snow white

  124. #F08080 LightCoral light coral color

  125. #BC8F8F RosyBrown rose brown

  126. #CD5C5C IndianRed Indian Red

  127. #FF0000 Red pure red

  128. #A52A2A Brown Brown

  129. #B22222 FireBrick firebrick/firebrick

  130. #8B0000 DarkRed deep red

  131. #800000 Maroon Maroon

  132. #FFFFFF White pure white

  133. #F5F5F5 WhiteSmoke White Smoke

  134. #DCDCDC Gainsboro light gray

  135. #D3D3D3 LightGrey light gray

  136. #C0C0C0 Silver silver gray

  137. #A9A9A9 DarkGray dark gray

  138. #808080 Gray gray

  139. #696969 DimGray Dim gray

  140. #000000 Black pure black'''

Guess you like

Origin blog.csdn.net/qq_27071221/article/details/130654025