python: concurrent programming (22)

foreword

This article will discuss with you the actual project of python concurrent programming: win graphical interface application (part four, 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 application of the scene.

This article is the twenty-second article of python concurrent programming. The address of the previous article is as follows:

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

The address of the next article is as follows:

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

1. Other controls

1、Progressbar

ttk.Progressbaris a control in Tkinter used to display a progress bar or loading bar, used to indicate the progress of a long-running task or the completion of an operation.

The following are ttk.Progressbarcommonly used properties and methods:

Common properties:

(1)mode: The mode of the progress bar, which can be determinate(deterministic) or indeterminate(deterministic).

(2)maximum: The maximum value of the progress bar.

(3)value: The current value of the progress bar.

(4)length: The length of the progress bar.

(5)orient: The direction of the progress bar, which can be horizontal(horizontal) or vertical(vertical).

Common methods:

(1)start(interval): Start the animation effect of the progress bar, and intervalspecify the update interval of the animation.

(2)stop(): Stop the animation effect of the progress bar.

(3)step(amount)amount: Increase the value of the progress bar by the specified value .

Here is a sample code that demonstrates how to create and use ttk.Progressbarthe control:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# 创建Progressbar控件
progressbar = ttk.Progressbar(root, mode="determinate", length=200)
progressbar.pack(pady=20)

# 更新进度条
def update_progress():
    value = progressbar["value"]
    if value < progressbar["maximum"]:
        value += 10
        progressbar["value"] = value
        root.after(500, update_progress)

# 启动进度条动画
progressbar.start()

# 模拟进度更新
root.after(500, update_progress)

root.mainloop()

In the above example, we created a ttk.Progressbarcontrol, set the mode of the progress bar to determinate(deterministic), and the length to 200. startThe animation effect of the progress bar is started through the method, and update_progressthe update process of the progress bar is simulated through the function.

Using ttk.Progressbarcontrols, we can visually display the progress of long-running tasks, providing user-friendly feedback. The mode, length and update method of the progress bar can be adjusted according to actual needs to meet specific application scenarios.

2、Separator

ttk.Separatoris a control in Tkinter used to create horizontal or vertical dividers for adding visual separation or grouping effects on the interface.

The following are ttk.Separatorcommonly used properties and methods:

Common properties:

(1)orient: The direction of the divider, which can be horizontal(horizontal) or vertical(vertical).

(2)style: The style of the divider.

Common methods: None.

Here is a sample code that demonstrates how to create and use ttk.Separatorthe control:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# 创建Separator控件
separator = ttk.Separator(root, orient="horizontal")
separator.pack(pady=20)

root.mainloop()

In the above example, we created a ttk.Separatorcontrol, set the direction of the divider to be horizontal ( orient="horizontal"), and then added it to the window.

Using ttk.Separatorcontrols, we can create visual dividers on the interface to increase the readability and aesthetics of the layout. The direction and style of the dividing line can be adjusted as needed to meet different interface design requirements.

3、Sizegrip

ttk.Sizegripis a control in Tkinter used to display a resizable handle in the corner of the window to facilitate the user to resize the window.

The following are ttk.Sizegripcommonly used properties and methods:

Common attributes: None.

Common methods: None.

Here is a sample code that demonstrates how to create and use ttk.Sizegripthe control:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# 创建Sizegrip控件
sizegrip = ttk.Sizegrip(root)
sizegrip.grid(row=1, column=1, sticky="se")

root.mainloop()

In the above example, we created a ttk.Sizegripcontrol and placed it in the lower right corner of the window (by grid()method setting rowand columnand stickyparameters). Users can use this control to resize the window.

Use ttk.Sizegripcontrols to enhance window resizing, enabling users to freely adjust the size of the window to suit their needs. This control is often used in conjunction with other controls to provide a better user experience.

4、Treeview

ttk.TreeviewA control in Tkinter for displaying tree-structured data, similar to a tree view in a file browser or a table.

The following are ttk.Treeviewcommonly used properties and methods:

Common properties:

(1)columns: TreeviewThe column name to set, which can be a string or a list of strings.

(2)displaycolumns: Set the columns to display, defaults to columnsall columns in .

(3)height: Set Treeviewthe number of visible rows.

(4)show: Set the elements to be displayed, such as table headers, tree symbols, etc.

(5)selectmode: Set the selection mode, the optional values ​​are "browse", "extended"or "none".

Common methods:

(1)insert(parent, index, iid=None, **kwargs): Insert a new item or subitem.

(2)delete(*items): Delete the specified item or subitem.

(3)item(item, option=None, **kwargs): Get or set the option value of an item or subitem.

(4)selection(): Get the currently selected item or subitem.

(5)focus(item=None): Set or get the focused item or subitem.

(6)bind(sequence, func, add=None): Bind event handler function.

Here is a sample code that demonstrates how to create and use ttk.Treeviewthe control:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# 创建Treeview控件
tree = ttk.Treeview(root, columns=("Name", "Age"))
tree.pack()

# 添加表头
tree.heading("#0", text="ID")
tree.heading("Name", text="Name")
tree.heading("Age", text="Age")

# 添加数据
tree.insert("", "end", text="1", values=("John Doe", 30))
tree.insert("", "end", text="2", values=("Jane Smith", 25))

root.mainloop()

In the above example, we created a ttk.Treeviewcontrol and defined two column names namely "Name"and "Age". We then use insert()the method Treeviewto insert two items into it, each item containing an ID and corresponding name and age.

Controls can be used ttk.Treeviewto easily display and manage tree-like data, providing flexible configuration options and rich methods to manipulate data and control display effects. You can adjust the style, event binding and data operation of the control according to your own needs to achieve the required functions and interactive experience.

5. Ttk style

Ttk (Themed Tkinter) is a module of Tkinter, which provides a set of themed controls, so that the application has a unified appearance and style on different platforms. The appearance and style of Ttk controls can be customized by using themes.

The following are some commonly used Ttk style-related methods and properties:

(1)ttk.Style(): Create a style object.

(2)style.configure(style_name, **options): Configure the attributes of the specified style, and you can set different style options through keyword parameters.

(3)style.map(style_name, query_opt, value_opt, **kwargs): Configure the style option map, which is used to set different style values ​​in different states.

(4)style.layout(style_name, layout): Configuration style layout, used to specify the arrangement of control components.

The following is a sample code that demonstrates how to use Ttk styles to customize the appearance and style of controls:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# 创建样式对象
style = ttk.Style()

# 配置样式
style.configure("TButton", foreground="red", font=("Helvetica", 12, "bold"))

# 创建按钮并应用样式
button = ttk.Button(root, text="Click Me", style="TButton")
button.pack()

root.mainloop()

In the above example, we first created an ttk.Styleobject and then used configure()the method to configure the style named "TButton". We set the foreground color of the button to red and use the bold font "Helvetica" with a font size of 12. Next, we create a ttk.Buttonbutton and set the style to "TButton".

By using Ttk styles, we can easily customize the appearance and style of controls. You can achieve the desired style effect by configuring the properties of the style, mapping different state values, and specifying the layout method. Ttk style provides a wealth of options, including font, color, border, padding, etc., which can be flexibly customized according to the needs of the application.

Guess you like

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