foreword
This article will discuss with you the actual project of python concurrent programming: win graphical interface application (part three, 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 twenty-first article of python concurrent programming. The address of the previous article is as follows:
Python: Concurrent Programming (20)_Lion King's Blog-CSDN Blog
The address of the next article is as follows:
Python: Concurrent Programming (22)_Lion King's Blog-CSDN Blog
1. Basic operation
1. tkinter.ttk --- Tk style controls
tkinter.ttk
is a module in Tkinter that provides a set of Tk-style controls for creating more modern and beautiful user interfaces.
tkinter.ttk
The module contains many controls corresponding to the standard Tkinter controls, such as Button
, Label
, , Entry
etc. They are named in a similar way to the standard controls, but with a "T" prefix added in front. For example, Button
controls tkinter.ttk
become in ttk.Button
.
Compared with standard controls, these Tk-style controls have better appearance and style, and can maintain a consistent appearance under different operating systems and themes. Using Tk-style controls can make applications have better user experience on different platforms.
In addition to having a better appearance, tkinter.ttk
the module also provides some other functions, such as customization of theme style, management of control state, enhancement of layout manager, etc. It also supports some new controls, such as Combobox
(drop-down box), Notebook
(tab), etc., and some extended controls, such as progress bar, tree view, etc.
To use tkinter.ttk
a module, just import it:
from tkinter import ttk
Then, you can ttk
use the Tk-style controls through the prefix.
It should be noted that since tkinter.ttk
the controls and functions provided by the module may have some differences from standard Tkinter, when writing an application, you need to choose to use standard controls or Tk-style controls according to the specific situation.
2. ttk control
ttk
Widgets are a set of controls in Tkinter, tkinter.ttk
provided through modules. These controls have a more modern, beautiful, and customizable look and feel that provides a consistent look across operating systems and themes.
Here are some common ttk
controls:
(1)ttk.Button
: Button control, used to respond to user click events.
(2)ttk.Label
: Label control, used to display text or image.
(3)ttk.Entry
: The input box control is used to receive the text entered by the user.
(4)ttk.Checkbutton
: Checkbox control, used to select one or more options.
(5)ttk.Radiobutton
: A radio button control used to select one of several options.
(6)ttk.Combobox
: Drop-down box control, which combines the functions of input box and drop-down list.
(7)ttk.Spinbox
: A spinner control for entering numeric values.
(8)ttk.Progressbar
: Progress bar control, used to display the progress of the task.
(9)ttk.Treeview
: Tree view control, used to display hierarchical data.
(10)ttk.Notebook
: Tab control, used to switch between different pages.
(11)ttk.Scale
: Slide bar control, used to select the value range.
(12)ttk.Separator
: Separator control, used to separate interface elements.
(13)ttk.Style
: Style manager, used to customize the appearance and style of the control.
These controls are used in a similar way to standard Tkinter controls, except that they are prefixed with "ttk.". By using ttk
controls, you can provide your application with a better user interface experience and be able to adapt to the appearance requirements of different platforms and themes.
Note that some special controls may require support from other modules, such as ttk.Treeview
using tkinter.ttk.Treeview
modules.
3、Combobox
ttk.Combobox
A control in Tkinter that combines the functionality of an input box and a drop-down list, allowing the user to select from predefined options or enter text manually.
The following are ttk.Combobox
commonly used properties and methods:
Common properties:
(1)values
: Specifies the option values in the dropdown list, which can be a list of strings or a tuple.
(2)textvariable
: A variable associated with the input box, used to get or set the text in the input box.
(3)state
: Specifies the state of the control, which can be "readonly"
(read-only) or "normal"
(editable).
Common methods:
(1)get()
: Get the text in the input box.
(2)set(value)
: Set the text in the input box to the specified value.
(3)current(index)
: Set the index of the currently selected item.
(4)bind(event, handler)
: Bind event processing function, trigger the corresponding processing function when the specified event occurs.
Here is a sample code that demonstrates how to create and use ttk.Combobox
the control:
import tkinter as tk
from tkinter import ttk
def on_select(event):
selected_value = combobox.get()
print("Selected value:", selected_value)
root = tk.Tk()
# 创建Combobox控件
combobox = ttk.Combobox(root, values=["Option 1", "Option 2", "Option 3"])
combobox.pack()
# 绑定事件处理函数
combobox.bind("<<ComboboxSelected>>", on_select)
root.mainloop()
In the above example, we created a ttk.Combobox
control specifying the value of the option in the drop-down list ["Option 1", "Option 2", "Option 3"]
. Then, we bind "<<ComboboxSelected>>"
the event, and when the user selects an option, on_select
the function is triggered and the selected value is printed.
By using ttk.Combobox
controls, we can easily implement input boxes with drop-down options to provide better user interaction and selection experience.
4、Spinbox
ttk.Spinbox
It is a control in Tkinter, which provides an input box that can increase or decrease the value by itself.
The following are ttk.Spinbox
commonly used properties and methods:
Common properties:
(1)from_
Or from
: Specify the minimum value that is allowed to be entered.
(2)to
: Specifies the maximum value allowed for input.
(3)increment
Or increment
: Specify the step size for each increase or decrease.
(4)textvariable
: A variable associated with the input box, used to get or set the value in the input box.
Common methods:
(1)get()
: Get the value in the input box.
(2)set(value)
: Set the value in the input box to the specified value.
Here is a sample code that demonstrates how to create and use ttk.Spinbox
the control:
import tkinter as tk
from tkinter import ttk
def on_change():
selected_value = spinbox.get()
print("Selected value:", selected_value)
root = tk.Tk()
# 创建Spinbox控件
spinbox = ttk.Spinbox(root, from_=0, to=10, increment=1)
spinbox.pack()
# 绑定变量的值改变事件
spinbox.bind("<<Increment>>", lambda _: on_change())
spinbox.bind("<<Decrement>>", lambda _: on_change())
root.mainloop()
In the above example, we created a ttk.Spinbox
control, specified the allowable value range from 0 to 10, and the step size of each increase or decrease is 1. Then, we bind the "<<Increment>>"
and "<<Decrement>>"
event, and when the user clicks the increase or decrease button, on_change
the function is triggered to print the current value.
By using ttk.Spinbox
controls, we can easily implement an input box with a value that can be incremented or decremented to provide a better user input and selection experience.
5、Notebook
ttk.Notebook
is a control in Tkinter for creating widgets with multiple tabs, similar to a tabbed interface.
The following are ttk.Notebook
commonly used properties and methods:
Common properties:
(1)style
: Specifies the style of the tab.
(2)tabs
: Returns a list of the names of all tabs.
Common methods:
(1)add(child, **kwargs)
: Adds a subcomponent to the tab. child
is the subcomponent to add, kwargs
and is the configuration option for setting the tab, such as text
for setting the title of the tab.
(2)forget(tab_id)
: Hide the specified tab.
(3)select(tab_id)
: Select the specified tab.
(4)tab(tab_id)
: Returns the widget for the specified tab.
(5)enable_traversal()
: Enables the ability to switch tabs by pressing a key.
Here is a sample code that demonstrates how to create and use ttk.Notebook
the control:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# 创建Notebook控件
notebook = ttk.Notebook(root)
# 创建第一个选项卡
frame1 = ttk.Frame(notebook)
frame1.pack(fill="both", expand=True)
label1 = ttk.Label(frame1, text="Content of Tab 1")
label1.pack(padx=10, pady=10)
notebook.add(frame1, text="Tab 1")
# 创建第二个选项卡
frame2 = ttk.Frame(notebook)
frame2.pack(fill="both", expand=True)
label2 = ttk.Label(frame2, text="Content of Tab 2")
label2.pack(padx=10, pady=10)
notebook.add(frame2, text="Tab 2")
notebook.pack(fill="both", expand=True)
root.mainloop()
In the above example, we created a ttk.Notebook
control and added two tabs (Tab 1 and Tab 2). Each tab contains a ttk.Frame
control for placing the content of the tab. We use add
methods to ttk.Frame
add controls to the tab and text
properties to set the tab's title.
By using ttk.Notebook
controls, we can easily create windows with a tabbed interface to organize and switch between different content pages conveniently.