Realization of Input Box and Text Box of Tkinter Window in python

  • 2021-10-27 07:51:59
  • OfStack

When making the login page, I learned the use of the input text box of TK pop-up window. Let's learn the use of the input box and text box in TK pop-up window.

Input box

If you want to make an input pop-up window is actually relatively simple, you only need a few lines of code, the following first look at the code that constitutes the input box


# Definition 1 Input text boxes 
# entry = tk.Entry(window, show="*")
# Represents the characters entered with the * Appear in the form of number 

entry = tk.Entry(window, show=None)
# Packaging the contents of a text box 
entry.pack()

The above lines of code are the code formed by the text box, but we also need to read the input content of the text box, which requires the use of the function statement: var = entry. get (), and also needs to define the window. After completion, the code is as follows:


# Import first tk
import tkinter as tk

# Define Window 
window = tk.Tk()
window.title('BIN Information management system ')
window.geometry('600x400')
# Definition 1 Input text boxes 
# entry = tk.Entry(window, show="*")
# Represents the characters entered with the * Appear in the form of number 
entry = tk.Entry(window, show=None)
# Packaging the contents of a text box 
entry.pack()
# Assigns the input character to the var
var = entry.get()

window.mainloop()

Text box

A text box is similar to an input box but different from an input window. It can be understood that a text window is used to print something but can also perform input operations. The specific code is as follows:


# Definition 1 Text boxes 
t = tk.Text(window, height=2)

t.pack()
window.mainloop()

Input box + text box

The following is a program combining an input box and a text box, which can realize input printing, insertion in different positions and other operations. The specific codes are as follows:


# Import first tk
import tkinter as tk

# Define Window 
window = tk.Tk()

window.title('BIN Information management system ')

window.geometry('600x400')

# Definition 1 Input text boxes 
# entry = tk.Entry(window, show="*")
# Represents the characters entered with the * Appear in the form of number 
entry = tk.Entry(window, show=None)
# Packaging the contents of a text box 
entry.pack()

# Definition 1 Functions inserted in the mouse position 
def insert_point():
    var = entry.get()
    t.insert('insert', var)
    
# Define the function inserted to the end 
def insert_end():
    var = entry.get()
    t.insert('end', var)
    #t.insert(2.2, var)  # Insert into the specified column 
# Definition button 
b1 = tk.Button(window, text=' Insert into the specified location ', width=15,height=2, command=insert_point)
# Pack button 
b1.pack()

b2 = tk.Button(window, text=' Insert to last ',command=insert_end)

b2.pack()
t.pack()

window.mainloop()

Related articles: