Python graphical interface basics: adding a text box (Entry) to the Tkinter window

introduction

In this article on the basics of Python graphical interface, we will focus on how to add a text box ( Entry ) in Tkinter . The text box is a common GUI element used to receive text information entered by the user. Whether you are creating a login screen, search box, or data entry form, text boxes are indispensable. In this article, we will explain in detail how to add a text box in a Tkinter window, and how to obtain and process user-entered text information.

What is a Tkinter text box (Entry)?

Tkinter 's text box ( Entry ) is a GUI element used to receive user input text . It allows users to enter text in a window and is usually used to receive single lines of text, such as usernames, passwords, search keywords, etc. A text box typically provides an editable text area in which the user can enter text, and the application can then obtain and process this input.

Let's start learning how to add a text box in a Tkinter window.

Step 1: Import the Tkinter module

First, make sure you have Python installed and include the Tkinter library. Then, import the Tkinter module in your Python script to use the functionality of the Tkinter library.

import tkinter as tk

Step 2: Create Tkinter window

Before using Tkinter , you need to create a Tkinter window object, usually called root. This window will serve as the main window of the GUI application.

root = tk.Tk()

Step 3: Create text box (Entry)

To create a text box, you use Entrythe class. Here's an example of creating a simple text box:

entry = tk.Entry(root)

In the above example, we created a textbox object and attached it to rootthe window. This will create an empty text box into which the user can enter text.

Step 4: Get the contents of the text box

An important use of text boxes is to obtain text entered by the user. You can use get()the method to get the contents of the text box. Here is an example:

text = entry.get()

In this example, we use get()the method to get the text in the text box and store it in the variable text. You can then use this text for processing or display.

Step 5: Add text box to window

Once the text box is created, pack()it needs to be added to the window using the method. This will determine the position of the text box in the window.

entry.pack()

pack()Method automatically adjusts the position of the text box based on the size and content of the window.

Complete sample code

Here is a complete sample code that demonstrates how to create a Tkinter window and add a textbox to it, and get the contents of the textbox when a button is clicked:

import tkinter as tk

# 创建Tkinter窗口
root = tk.Tk()
root.title("Tkinter文本框示例")

# 创建文本框
entry = tk.Entry(root)

# 创建按钮点击事件处理程序
def button_click():
    text = entry.get()
    label.config(text="你输入的文本是:" + text)

# 创建按钮
button = tk.Button(root, text="获取文本", command=button_click)

# 创建标签
label = tk.Label(root, text="")

# 将文本框、按钮和标签添加到窗口
entry.pack()
button.pack()
label.pack()

# 启动Tkinter主事件循环
root.mainloop()

Rendering:
Insert image description here

Code explanation

Let us explain the above code line by line:

  • First, we imported the Tkinter module in order to use the functionality of the Tkinter library.

  • Next, we created a Tkinter window object rootand set the title of the window to " Tkinter TextBox Example".

  • We then create a text box object entryand attach it to rootthe window where the user can enter text.

  • We define a button_clickbutton click event handler named which will be executed when the button is clicked. In this example, we use get()the method to get the text in the text box and display it on the label.

  • We create a button button, set the text on the button to "get text", and button_clickassociate an event handler with the button's click event.

  • Finally, we create a label labelthat displays the fetched text.

  • We pack()added text boxes, buttons, and labels to the window using the method and started Tkinter 's main event loop.

Customize text box properties

In addition to the basic text box, you can also customize the appearance and behavior of the text box. You can set the width, height, font, background color, foreground color, etc. of the text box. Here's an example of how to customize the properties of a text box:

# 创建一个自定义样式的文本框
custom_entry = tk.Entry

(
    root,
    width=30,                  # 设置文本框宽度
    font=("Helvetica", 14),    # 设置字体和字号
    bg="lightyellow",          # 设置背景颜色
    fg="navy"                  # 设置前景颜色(文本颜色)
)

# 将自定义文本框添加到窗口
custom_entry.pack()

Rendering:
Insert image description here

In the above example, we created a custom styled text box, setting the width, font, background color, and foreground color.

in conclusion

In this article, we learned how to add a text box in a Tkinter window and how to get the text information entered by the user. Text boxes are an indispensable element in GUI applications, used to receive text entered by the user. By creating and customizing text boxes, you can add more interactivity and functionality to your application. In the following tutorials, we will continue to learn how to add additional GUI elements, handle different types of events, and build richer and more powerful GUI applications. Keep learning and you'll be able to create more interesting and useful GUI applications!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/132839065