python: concurrent programming (twenty)

foreword

This article will discuss with you the actual project of python concurrent programming: win graphical interface application (part 2, a total of eight articles) . The series of articles will build the project from scratch, gradually improve the project, and finally make the project suitable for high concurrency scene application.

This article is the twentieth article of python concurrent programming. The address of the previous article is as follows:

Python: Concurrent Programming (19)_Lion King's Blog-CSDN Blog

The address of the next article is as follows:

Python: Concurrent Programming (21)_Lion King's Blog-CSDN Blog

1. Quick start of Tkinter

Official documentation: Tk Graphical User Interface (GUI) — Python 3.11.4 documentation

This chapter simply learns how to use Tkinter interface elements. The reason why it is simple is that we will learn it because it will be used in subsequent articles. Even some of the concepts used later will not necessarily be mentioned, and some concepts will not be used. But it must be said.

 1. Tkinter Architecture

Tkinter consists of three relatively independent libraries TCL, TK, and TTK. Tcl is a scripting language, Tk is a cross-platform graphical user interface toolkit, and TtK is a TK enhancement module with themes, so the three are called Tcl/Tk architecture. Tkinter is based on Tcl/Tk, and it uses the underlying functions provided by Tcl/Tk to create and manage graphical interfaces.

2. Tkinter module

Tkinter is a standard graphical user interface (GUI) toolkit for Python. It provides functions for creating various interface elements such as windows, buttons, labels, and text boxes, enabling developers to easily create graphical interface applications. The following are some basic knowledge points of the Tkinter module:

(1) Import Tkinter module

import tkinter as tk

(2) Create the main window

window = tk.Tk()

(3) Set the window title

window.title("窗口标题")

(4) Add tags

label = tk.Label(window, text="标签文本")
label.pack()

(5) Add button

button = tk.Button(window, text="按钮文本")
button.pack()

(6) Add a text box

entry = tk.Entry(window)
entry.pack()

(7) Add a menu bar

menubar = tk.Menu(window)
window.config(menu=menubar)

(8) Create menu items

file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="文件", menu=file_menu)

(9) Binding event handler function

def button_click():
    # 处理按钮点击事件的代码

button = tk.Button(window, text="按钮文本", command=button_click)

(10) Run the main loop

window.mainloop()

The above are some basic usages of Tkinter modules. You can further study and explore various functions and components of Tkinter according to your needs, so as to create graphical interface applications that meet your requirements.

3. Tkinter Supplements

There are some other important aspects and features to know when using Tkinter for GUI development. Here are some Tkinter pick-ups:

(1) Styles and Themes (Styles and Themes): Tkinter provides support for styles and themes, and you can customize the appearance and style of the application by setting different styles and themes. You can use ttk.Styleobjects to define and apply styles, and use predefined themes to change the overall look.

(2) Dialogs and Message Boxes (Dialogs and Message Boxes): Tkinter provides some built-in dialog boxes and message boxes for displaying warnings, errors, information, etc. You can use tkinter.messageboxmodules to create and display these dialog boxes, such as prompt boxes, confirmation boxes, input boxes, etc.

(3) Drawing and Canvas: Tkinter allows you to perform drawing operations on the Canvas, including drawing lines, rectangles, ellipses, polygons, etc. You can use tkinter.Canvascontrols to create canvases and draw on them using various methods and properties.

(4) Menus and Toolbars: Tkinter supports the creation of menu bars and toolbars for organizing and displaying various functions and options of the application. You can use tkinter.Menuand tkinter.Menucontrols to create menus and submenus, and tkinter.Toolbarcontrols to create toolbars.

(5) Keyboard Shortcuts (Keyboard Shortcuts): Tkinter allows you to define keyboard shortcuts for menu items and other controls to provide a more convenient way of operation. By using specific keybinding rules, you can associate specific keys with specific functions.

(6) Window Management (Window Management): Tkinter allows you to create and manage multiple windows, and switch and interact between windows. You can use tkinter.Toplevelcontrols to create additional top-level windows, and use methods such as lift()and lower()to adjust the hierarchical order of windows.

(7) Internationalization Support (Internationalization Support): Tkinter provides internationalization (i18n) and localization (l10n) support, allowing you to create multilingual applications. By using gettextmodules and associated localization files, you can translate your application's text and labels into different languages.

These are just some of the highlights of Tkinter, there are many other functions and features that can be explored and applied. Using Tkinter for GUI development needs to be familiar with the various controls, methods and properties it provides, and customize and develop according to your own needs.

4. Important Tk concepts

In Tkinter, there are some important Tk concepts to know in order to better understand and use Tkinter. Here are some important Tk concepts:

(1) Root Window (Root Window): The topmost window of a Tkinter application is called the root window. It is the parent window of all other windows and widgets. Create the root window by creating tkinter.Tkan object.

(2) Widgets: Tkinter provides various widgets for building GUI interfaces. Widgets can be buttons, labels, text boxes, check boxes, scroll bars, etc. You can create and configure widgets using the corresponding Tkinter classes.

(3) Geometry Management: Tkinter provides several methods to manage the layout and position of widgets. The most commonly used methods are Pack Manager, Grid Manager and Place Manager.

(4) Event-Driven (Event-Driven): Tkinter is an event-driven library, which means it responds to user operations and events. You can bind various events (such as click, mouse movement, keyboard input, etc.) to widgets and define corresponding event handlers.

(5) Variables (Variables): Tkinter provides some special variable classes for sharing data between widgets and applications. The most commonly used ones are tkinter.StringVar, , tkinter.IntVarand tkinter.BooleanVar.

(6) Callback Functions (Callback Functions): A callback function is a function that is called when a specific event occurs. In Tkinter, you can pass functions as parameters to related methods of widgets to implement event handling and function extension.

(7) Graphical Context: Tkinter uses a graphics context to draw graphics and text. You can use widgets such as Canvas or Label to display content in the graphics context.

These are some important concepts in Tkinter, understanding them will help you better build GUI applications using Tkinter. By becoming familiar with these concepts and continuing to practice, you will be able to create feature-rich and interactive GUI interfaces.

5. Tkinter threading model

Tkinter adopts a single-threaded model, that is, all Tkinter-related operations should be performed in the main thread. This is because Tkinter is based on a Python interface to the Tk library, which itself is not thread-safe. Therefore, both the main event loop (main loop) and GUI update operations of a Tkinter application need to be performed in the main thread.

In Tkinter, the main thread is responsible for handling GUI events and updating the GUI interface. When the user interacts with the interface, triggers events, or performs GUI updates, these operations will be added to the event queue, and the main thread will process the events in the queue in turn and update the interface. Therefore, if you perform Tkinter-related operations in other threads, it may cause thread safety issues and unpredictable behavior.

If you need to perform time-consuming tasks or long calculations in your Tkinter application, you can use the threadingmodule to create new threads and perform these tasks in the new threads. Then, by using the inter-thread communication mechanism provided by Tkinter, the result is passed back to the main thread and the GUI interface is updated. Commonly used inter-thread communication mechanisms include tkinter.Variable, , tkinter.afterand tkinter.Event.

It should be noted that when accessing Tkinter widgets across threads, appropriate thread synchronization mechanisms must be used to avoid thread conflicts and data races. Can be guarded using threading.Lockor etc. thread synchronization objects.threading.RLock

To sum up, Tkinter adopts a single-threaded model, all Tkinter-related operations should be performed in the main thread, and time-consuming tasks can be performed in other threads, and interact with the main thread and update the GUI interface through the inter-thread communication mechanism .

6. Hello World program

from tkinter import *
from tkinter import ttk
root = Tk()
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
root.mainloop()

Guess you like

Origin blog.csdn.net/weixin_43431593/article/details/131354761