Python graphical interface basics: opening and closing new windows

introduction

In Python graphical user interface ( GUI ) applications, creating and managing multiple windows is an important task. These windows can be used for different purposes, such as displaying additional information, performing specific actions, or otherwise improving the user experience. In this article, we’ll take a deep dive into how to use Python ’s Tkinter library to open and close new windows, and demonstrate how to implement these features in your application.

Introduction to the Tkinter library

Tkinter is a module in the Python standard library for creating GUI applications. It provides a set of tools and components for building GUI elements such as windows, buttons, labels, and text boxes, and supports event handling mechanisms that can monitor and respond to user interactions.

Step 1: Import the Tkinter module

First, make sure your Python environment has the Tkinter library installed . 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

When using Tkinter to create a GUI application, you first need to create a Tkinter window object, usually called root. This window will serve as the application's main window.

root = tk.Tk()
root.title("打开和关闭新窗口示例")

In the code above, we create a Tkinter window object rootand set the title of the window to "Open and Close New Window Example".

Step 3: Create a new window

To create a new window we need to create a new Tkinter window object. This new window can contain anything we want to display, such as labels, buttons, text boxes, etc.

Here's an example of how to create a new window and add a label:

def open_new_window():
    new_window = tk.Toplevel(root)
    new_window.title("新窗口")
    
    label = tk.Label(new_window, text="这是一个新窗口")
    label.pack()

# 创建一个按钮,用于打开新窗口
open_button = tk.Button(root, text="打开新窗口", command=open_new_window)
open_button.pack()

In the above example, we defined a open_new_windowfunction called which creates a new Tkinter window object new_windowand sets the window's title to "New Window". We then added a label to the new window labelthat displays the text "This is a new window". Finally, we created a button open_buttonthat when the user clicks the button will call open_new_windowa function to open a new window.

Step 4: Close the new window

To close the new window, we can use destroy()the method to destroy the window object. This will close the window and release the resources associated with it.

Here's an example of how to add a close button to a new window so the user can close the window:

def close_window(window):
    window.destroy()

# 在新窗口中创建一个关闭按钮
close_button = tk.Button(new_window, text="关闭窗口", command=lambda: close_window(new_window))
close_button.pack()

In the above example, we defined a close_windowfunction called which accepts a window object windowas a parameter and destroy()closes the window using the method. We then created a button in the new window close_buttonand when the user clicks the button, close_windowthe function is called to close the new window.

Step 5: Start Tkinter main event loop

The final step is to start Tkinter 's main event loop, which will make the window interactive, allowing the user to open and close new windows.

root.mainloop()

Complete sample code

Here is a complete example code showing how to create a Tkinter window, buttons, and how to open and close new windows:

import tkinter as tk

def open_new_window():
    new_window = tk.Toplevel(root)
    new_window.title("新窗口")
    
    label = tk.Label(new_window, text="这是一个新窗口")
    label.pack()

def close_window(window):
    window.destroy()

# 创建Tkinter窗口
root = tk.Tk()
root.title("打开和关闭新窗口示例")

# 创建一个按钮,用于打开新窗口
open_button = tk.Button(root, text="打开新窗口", command=open_new_window)
open_button.pack()

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

Rendering:
Insert image description here
Insert image description here

Code explanation

Let us explain the above code line by line:

  • We imported the Tkinter module in order to use the functionality of the Tkinter library.

  • Created a Tkinter window object rootand set the title of the window to "Open and Close New Window Example".

  • A function called is defined open_new_window, which creates a new Tkinter window object new_windowand adds a label to it labelthat displays the text "This is a new window".

  • A button is created open_buttonand when the user clicks the button, open_new_windowa function is called to open a new window.

  • A function named is defined in the new window close_window, which accepts a window object windowas a parameter and uses destroy()the method to close the window.

  • A button is created in the new window close_button, and when the user clicks the button, close_windowthe function is called to close the new window.

  • Finally, Tkinter 's main event loop is started , making the window interactive.

in conclusion

In this article, we learned how to open and close new windows using Python ’s Tkinter library. Creating and managing multiple windows is an important part of GUI application development and can be used to improve the user experience, display additional information, or perform specific actions. The Tkinter library provides a wealth of tools and methods for creating and controlling multiple windows, enabling you to build more complex and interesting GUI applications to meet the needs of different users. Continuing to learn Tkinter , you will be able to develop more interactive and powerful applications.

Guess you like

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