This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Review and

After we played in the Nuggets community for a period of time, we met a lot of rich introductions of big guys, then we also go to change the profile, click on the profile page below

  • Our avatar and left text display is the Label Label
  • From the user name bar to the personal home page bar is the Entry single-line text box
  • Personal introduction box supports multi-line Text input is Text multi-line Text box
  • Finally, after modifying the above information, click save to modify the Button is Button

Take a look. What’s the difference between the profile column and the other columns besides the size and shape?

Let’s go: The Python Tkinter component supports multiple Text fields

1. Text Component Overview

Text Multi-line Text box, used to receive multiple lines of Text

  • Edit pages as “rich text” and need to enter a large number of strings

  • You can display web links, images, HTML pages, even CSS styles, add components, etc

The syntax of Text is as follows:

Txt = Text(master, Options)Copy the code

2. Compare Text with Entry

Entry and Text are standard components of Python Tkinter, and are input box components that accept user input.

  • The same

    1. Both Entry and Text provide a get() method to get the contents of a Text box
    2. Both Entry and Text provide insert() methods to change the contents of a Text box
    3. Both Entry and Text support indexes
  • The difference between

    1. Entry can be entered in a single line, and Text can be entered in multiple lines
    2. You cannot add different formats for Entry. You can add different formats for different parts of Text
    3. The index of Entry is simple, for example, (3,8) indicates the fourth character to the eighth character. The index of Text needs to specify both the row number and column number (1.3, 2.6) indicates the fourth character on the first line and the seventh character on the second line
    4. Entry supports bidirectional binding. Can you bind an Entry to a variable? You can also change or obtain the contents of the Entry component through variables. Text is not supported and can only be bound to variables

3. Common properties of the Text component

attribute meaning
height Sets the height of the textbox. Each increment of the height value adds one line
width Sets the width of the textbox, incrementing the value by 1 byte
insert Text box to insert data, you can specify the location to insert data
delete Delete data in the text box. You can specify the data to be deleted according to the data location
get Get the data in the text box, you can specify the data to be obtained by the data location
relief Textbox style, set the display effect of controls. Options: FLAT, SUNKEN, RAISED, GROOVE, RIDGE.
bd Sets the border size of the text box. The larger the value, the wider the border
bg Sets the default background color of the text box
fg Sets the default foreground color of the text box, the font color
font Text font, text size, text glyphs. Glyph overstrike/italic/bold/underline
state The value can be DISABLED or NORMAL. The value cannot be entered in the DISABLED state text box, but can be entered in the NORMAL state
highlightcolor Sets the border color of the text box after it is clicked
highlightthickness Sets the border size of the text box after it is clicked

4. The Text component provides common methods

methods role
bbox(index) Displays the specified character screen area
compare(index1,op,index2) Compares whether characters in two positions satisfy the specified relationship
Count (index1 index2) Computes the distance between the specified positions
delete(index) Delete the specified string
dlineinfo(index) Returns the row occupied area at the specified location
dump(index) Saves the contents of the specified location
edit(*args) Edits content at the specified location
edit_modified() Edit and modify content
edit_redo() Undo the edited content
get(index) Gets the content at the specified location
image_cget() Gets the content of the embedded image
image_names() Gets the name of the image
insert(index,chars) Inserts new content to the specified location
mark_names The tag information
peer_names Create the name of the query component
search(pattern,index) Queries content in a specified range
window_create(index)

5. Test the cat

Text Related methods exercise results are as follows:

  1. Create a Text component object whose properties can be set using Option.

    For example, create a Text component with a pink background color

    self.txt = Text(root,width = 40,height = 12,bg ="pink")
    Copy the code
  2. The Text component supports adding multiple lines of Text, which can be added using the INSERT () method

    Def insertText(self): self.txt. Insert (insert,' cute ') self.txt. Insert (END,'celebrating')Copy the code
  3. The Text component supports adding images, using image_create()

    def addImage(self):
    
        self.photo = PhotoImage(file = "heart.gif")
        self.txt.image_create(END,image = self.photo)
    Copy the code
  4. The Text component adds other components, using the window_create() method

    Def addWidget(self): b1 = Button(self.txt,text = "Button ") self.txt. Window_create (INSERT,window = b1)Copy the code
  5. The Text component empties the string, using the delete() method

    Def resetText (self) : self. TXT. Delete (1.0, END)Copy the code

    📣 INSET indicates the cursor position, and END moves the cursor to the END of the text

  6. Using GUI object-oriented programming, use Application(Frame) to create the instance object, encapsulate the method to create the component with createWidgetText(), and create the component with the constructor

  7. The final complete code is as follows:

from tkinter import * class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Super ().__init__(master) self.master = master self.pack() self.creatwidgettext () def CreatWidgetText(self): Self.txt = Text(root,width = 40,height = 12,bg ="pink") self.txt. Pack () self.txt. Insert (1.0,"1234567890 \n DSDDD ") Insert (2.0,"welcome to juejin everyone ") Button(self,text = "COPY",command = self.inserttext).pack(side = "left") Button(self, text="BACK", command=self.returnText).pack(side="left") Button(self, text="AddImage", command=self.addImage).pack(side="left") Button(self, text="AddWidget", command=self.addWidget).pack(side="left") Button(self, text="RESET", command=self.resetText).pack(side="left") def insertText(self): Self.txt. Insert (insert,'celebrating') def returnText(self): Print (self.txt.get(1.2,1.6)) self.txt.insert(3.0," ") print("All text:\n"+self.txt.get(1.0,END)) def addWidget(self): B1 = Button(self.txt,text = "") self.txt. Window_create (INSERT,window = b1) def addImage(self): self.photo = PhotoImage(file = "heart.gif") self.txt.image_create(END,image = self.photo) def resetText(self): Self.txt.delete (1.0,END) root = Tk() root. Geometry ("400x300+200+300") root. Title ("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code

conclusion

In this installment, we’ll look at the text input field of the Tkinter component

  • The Entry single-line text box is used to fill in forms such as user name and password on the login page
  • Text A multi-line Text box is used as a rich Text box and supports multiple Text formats

In a work scenario that needs to receive user input strings, we can choose to use Entry and Text components based on the amount of input, and they also provide methods for deleting, modifying, and inserting Text.

The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~ღ(´ ᴗ · ‘) than xin 🌹