README.md 27.1 KB
Newer Older
小康2022's avatar
小康2022 已提交
1
<div align="center">
小康2022's avatar
小康2022 已提交
2
    <h1>🚀tkintertools🚀</h1>
Logo  
小康2022 已提交
3
    <p><img height="128px" alt="logo.png" src="tkt.png" /></p>
小康2022 已提交
4 5 6 7
    <p>
    The <code>tkintertools</code> module is an auxiliary module of the <code>tkinter</code> module
    <br />
    <code>tkintertools</code> 模块是 <code>tkinter</code> 模块的辅助模块
小康2022's avatar
小康2022 已提交
8 9
    </p>
    <p>
小康2022 已提交
10 11
        <a href="https://github.com/Xiaokang2022/tkintertools">
            <img src="https://img.shields.io/badge/Version-2.6.5-blue" alt="latest version" />
小康2022's avatar
小康2022 已提交
12
        </a>
小康2022 已提交
13
        <a href="LICENSE.txt">
小康2022's avatar
小康2022 已提交
14 15
            <img src="https://img.shields.io/badge/License-Mulan PSL v2-green" alt="License" />
        </a>
小康2022 已提交
16
        <a href="CHANGELOG.md">
小康2022 已提交
17
            <img src="https://img.shields.io/badge/ChangeLog-2023/06/17-orange" alt="ChangeLog" />
小康2022's avatar
小康2022 已提交
18
        </a>
小康2022 已提交
19
        <a href="TODO.md">
小康2022 已提交
20
            <img src="https://img.shields.io/badge/ToDo-14-yellow?logo=cachet" alt="ToDo" />
小康2022's avatar
小康2022 已提交
21
        </a>
小康2022 已提交
22 23
        <a href="https://github.com/Xiaokang2022/tkintertools/wiki">
            <img src="https://img.shields.io/badge/Wiki-15-purple" alt="Wiki" />
小康2022's avatar
小康2022 已提交
24
        </a>
小康2022 已提交
25
        <br />
小康2022 已提交
26
        <a href="https://pypistats.org/packages/tkintertools">
小康2022 已提交
27
            <img src="https://img.shields.io/badge/Download-7k-white?logo=pypi" alt="Downloads" />
小康2022's avatar
小康2022 已提交
28
        </a>
小康2022 已提交
29
        <a href="https://github.com/Xiaokang2022">
小康2022 已提交
30
            <img src="https://img.shields.io/badge/Author-小康2022-springgreen" alt="Author" />
小康2022's avatar
小康2022 已提交
31
        </a>
小康2022 已提交
32 33 34
        <a href="https://xiaokang2022.blog.csdn.net">
            <img src="https://img.shields.io/badge/Blog-小康2022@CSDN-red" alt="Blog" />
        </a>
小康2022 已提交
35 36 37
        <a href="mailto:2951256653@qq.com">
            <img src="https://img.shields.io/badge/Email-2951256653@qq.com-cyan" alt="Email" />
        </a>
小康2022's avatar
小康2022 已提交
38
    </p>
小康2022 已提交
39
    <p><img src="https://repobeats.axiom.co/api/embed/ab8fae686a5a96f91fa71c40c53c189310924f5e.svg" alt="Repobeats analytics image" /></p>
小康2022's avatar
小康2022 已提交
40 41
</div>

Logo  
小康2022 已提交
42 43
Install/模块安装
---------------
小康2022's avatar
小康2022 已提交
44

小康2022's avatar
小康2022 已提交
45
### Stable version/稳定版本
小康2022's avatar
小康2022 已提交
46

小康2022 已提交
47 48
* Version/版本 : `2.6.5`
* Release Date/发布日期 : 2023/06/17 (UTC+08)
小康2022's avatar
小康2022 已提交
49

小康2022 已提交
50 51
这个是目前的最新稳定版,比较稳定,bug(可能)没有那么多,推荐使用这个。  
稳定版 bug 不一定少,但它至少是我在多个操作系统中测试过的,是跨平台的,兼容的。
小康2022's avatar
小康2022 已提交
52

小康2022 已提交
53
```
小康2022 已提交
54
pip install tkintertools==2.6.5
小康2022 已提交
55 56
```

小康2022's avatar
小康2022 已提交
57 58
### Development version/开发版本

小康2022 已提交
59 60
* Version/版本 : `2.6.5`
* Release Date/发布日期 : 2023/06/13 (UTC+08)
小康2022's avatar
小康2022 已提交
61

小康2022 已提交
62
这个是我正在开发的版本,可能有新功能,bug 也可能会比较多,但也可能会比原来的版本更加稳定。  
小康2022 已提交
63 64
开发版没有经过多操作系统的测试,仅能保证在 Windows 系统下运行所有功能,在其他的操作系统上,可能有部分功能无法正常运行。  
大家可以在 Issues 中提出一些建议,我可能会适当采纳一些并在开发版本中更改或实现。
小康2022 已提交
65

小康2022's avatar
小康2022 已提交
66
```
小康2022 已提交
67
pip install tkintertools-dev==2.6.5
小康2022's avatar
小康2022 已提交
68 69
```

小康2022 已提交
70
**Attention/特别注意**
小康2022 已提交
71 72
* 开发版仅作示例,各函数或类的 API 并非最终确定结果,直接使用开发版可能导致后续无法与稳定版兼容!
* 若要使用开发版,请先卸载稳定版后再进行 pip 安装,再次使用稳定版时也是一样,先卸载开发版再安装稳定版!
小康2022 已提交
73 74 75

### Environmental requirements/环境需求

小康2022 已提交
76
没有任何额外的依赖包(除了 tkinter),但只支持以下 Python 版本:
小康2022 已提交
77 78 79 80 81 82 83 84 85 86 87 88

![Python3.8](https://img.shields.io/badge/Python-3.8.*-blue?logo=python)
![Python3.9](https://img.shields.io/badge/Python-3.9.*-blue?logo=python)
![Python3.10](https://img.shields.io/badge/Python-3.10.*-blue?logo=python)
![Python3.11](https://img.shields.io/badge/Python-3.11.*-blue?logo=python)
![Python3.12](https://img.shields.io/badge/Python-3.12.*-blue?logo=python)

可能在某些操作系统上也可运行,但目前以下操作系统已经测试通过:

![Windows10](https://img.shields.io/badge/Windows-10-green?logo=windows)
![Windows11](https://img.shields.io/badge/Windows-11-green?logo=windows11)
![Ubuntu22.04](https://img.shields.io/badge/Ubuntu-22.04-green?logo=ubuntu)
小康2022 已提交
89

小康2022 已提交
90
News/最新功能
小康2022 已提交
91
------------
小康2022 已提交
92

小康2022 已提交
93 94 95
**最新版本: tkintertools-2.6.5 稳定版**

**【重磅炸裂更新:新增了 Wiki 供大家查看,方便大家迅速掌握 tkintertools 的使用方法和小技巧![点此传送](https://github.com/Xiaokang2022/tkintertools/wiki)】**
小康2022 已提交
96

小康2022 已提交
97 98 99
    【特别说明】
    近期本人将暂时停止对 tkintertools 模块本身的更新,本人先去完善 Wiki 再继续更新 tkintertools!
    此外,README.md 中关于模块内容详细介绍的部分将逐步转移到 Wiki 中!
小康2022 已提交
100

小康2022 已提交
101
下面是本次版本更新内容条目:
小康2022 已提交
102

小康2022 已提交
103 104 105 106 107
- [X] 子模块 `tools_3d` 新增类 `Space` 可以直接提供对 3D 对象进行平移、旋转和缩放等操作的功能
- [X] 修复了类 `Cuboid` 和类 `Tetrahedron` 没有将实例添加到父类 `Canvas_3D` 的 bug
- [X] 修复了当 3D 对象出现在相机位置后面时会显示错误的 bug(目前更改为透视,即不显示)
- [X] 修复了部分错误的类型提示和错误的参数默认值
- [X] 移除常量 `BACKGROUND` 并且不再对类 `Canvas` 的默认背景颜色做限定
小康2022 已提交
108

小康2022 已提交
109
下面是一个主要新功能的示例程序,运行下面的示例程序时,其拥有以下功能:
小康2022 已提交
110 111
* 按住鼠标左键拖动可以旋转这多个几何体;
* 按住鼠标右键拖动可以移动这些几何体在空间中的位置;
小康2022 已提交
112
* 滚动鼠标中键可以放大和缩小画面;
小康2022 已提交
113
* 按“=”和“-”键分别可以放大和缩小几何体的大小;
小康2022 已提交
114

小康2022 已提交
115
下面是示例程序的效果图:
小康2022 已提交
116

小康2022 已提交
117
![news.png](news.png)
小康2022 已提交
118 119 120 121 122 123 124

<details><summary><b>源代码</b></summary>

```python
import tkintertools as tkt
from tkintertools import tools_3d as t3d

小康2022 已提交
125 126
root = tkt.Tk('tkintertools-2.6.5', 1280, 720)
cv3d = t3d.Space(root, 1280, 720, 0, 0)
小康2022 已提交
127

小康2022 已提交
128 129 130 131 132 133
for a in -100, 0, 100:
    for b in -100, 0, 100:
        for c in -100, 0, 100:
            t3d.Cuboid(cv3d, a-50, b-50, c-50, 100, 100, 100,
                       color_up='white', color_down='yellow', color_left='red',
                       color_right='orange', color_front='blue', color_back='green')
小康2022 已提交
134
cv3d.space_sort()
小康2022 已提交
135 136


小康2022 已提交
137
def scale(event):
小康2022 已提交
138
    """ 缩放事件 """
小康2022 已提交
139
    k = 1.05 if event.keysym == 'equal' else 0.95 if event.keysym == 'minus' else 1
小康2022 已提交
140
    for geo in cv3d.geos():
小康2022 已提交
141 142
        geo.scale(k, k, k)
        geo.update()
小康2022 已提交
143 144 145 146
    cv3d.space_sort()


root.bind('<Any-Key>', scale)
小康2022 已提交
147 148 149 150 151
root.mainloop()
```

</details>

小康2022 已提交
152
更多更新信息请见:[CHANGELOG.md](CHANGELOG.md)
小康2022 已提交
153

小康2022 已提交
154 155 156 157 158
---
**特别说明**

    后面的内容即将移植到 Wiki 中!

小康2022's avatar
小康2022 已提交
159
Description/模块说明
小康2022's avatar
小康2022 已提交
160
----------------------
小康2022's avatar
小康2022 已提交
161

小康2022's avatar
小康2022 已提交
162
tkintertools 是一款基于 tkinter 模块的二次开发的界面编程模块,它完全没有使用任何第三方模块和库的,同时,它也没有任何依赖包,它的功能完全由内置模块和函数实现,而且,它还是跨平台的!它和 tkinter 最大的不同在于,它的控件并非真实的控件,而是在 tkinter 模块中 Canvas 对象中绘制而成的,这就赋予了 tkintertools 控件一些在 tkinter 中没有的特性,列举如下:
小康2022's avatar
小康2022 已提交
163

小康2022's avatar
小康2022 已提交
164 165 166
* 控件背景可以是透明的(实际上是没有背景颜色)
* 控件的样式可以是自定义的(比如按钮有圆角)
* 控件的创建速度远大于 tkinter 的控件(除了 Canvas 控件)
小康2022's avatar
小康2022 已提交
167

小康2022's avatar
小康2022 已提交
168
但同时也产生了一些缺点:
小康2022's avatar
小康2022 已提交
169

小康2022's avatar
小康2022 已提交
170 171
* 虚拟的控件无法获取焦点
* 虚拟的控件在文本输入和显示的功能上存在一些缺陷(这个缺陷不是很明显,但强迫症就有点难受了,比如我)
小康2022's avatar
小康2022 已提交
172

小康2022's avatar
小康2022 已提交
173
tkintertools 模块还具有一些特色的功能:
小康2022's avatar
小康2022 已提交
174

小康2022's avatar
小康2022 已提交
175 176 177
* 利用 tkinter 和 tkintertools 创建的程序,在高分辨率的情况下,tkintertools 的会更加清晰(这点对于笔记本用户很友好,比如我)
* 可以迅速实现渐变色的效果
* 窗口缩放,所有的控件的大小跟着缩放(当然,也可以设置为不跟随缩放)
小康2022 已提交
178
* 子模块 tools_3d 可以是满足简单的 3D 绘图需求
小康2022's avatar
小康2022 已提交
179 180

Provides/模块功能
小康2022's avatar
小康2022 已提交
181
-------------------
小康2022's avatar
小康2022 已提交
182

小康2022's avatar
小康2022 已提交
183 184 185
Here, only the more distinctive features will be listed  
这里只会列举出比较具有特色的功能

小康2022's avatar
小康2022 已提交
186 187 188 189 190 191
### Customizable widgets/可自定义的控件

tkintertools 模块的控件拥有许多参数供我们设置,比如圆角的半径、文本和边框以及控件内部的颜色,关联事件等等。  
这里要说明的是,每个控件可以设置的关联事件不止一种,在鼠标经过控件时可以绑定事件,鼠标点击控件也可以,鼠标点击后松开也行等等。  
文本和边框以及控件的填充色也是类似的,在鼠标经过控件、点击控件、点击后松开都可以设定颜色。  
文本类控件还能够从右边逐步输入文本,文本输入提示符也可以不是单调无趣的竖线,可以是其他的,比如下划线等。  
小康2022 已提交
192
最后,大家可以看一下 [test.py](test.py) 文件里面的示例,这个示例展示了 tkintertools 模块的绝大部分功能,示例中更有隐藏的 “多彩变幻” 彩蛋哦!
小康2022's avatar
小康2022 已提交
193 194 195 196 197 198 199

### Automatically control size/自动控制大小

tkintertools 中的控件,其大小和形状可以随着窗口的变化而成比例地变化,不仅仅是控件中的文本,Canvas 绘制的图形也会随之变动,更让人兴奋的是,png 类型的图片也会随之成比例地缩放!当然,你也可以设定参数让其不随之变动,也可以设定参数使其在缩放的时候保持横纵方向的比例。  
总之,很方便,很舒适!

### Easily move widgets/轻松移动控件
小康2022's avatar
小康2022 已提交
200

小康2022's avatar
小康2022 已提交
201
[移动函数](#move)
小康2022's avatar
小康2022 已提交
202

小康2022's avatar
小康2022 已提交
203
### Gradient colors/渐变色
小康2022's avatar
小康2022 已提交
204

小康2022's avatar
小康2022 已提交
205
[颜色函数](#Gradient)
小康2022's avatar
小康2022 已提交
206

小康2022's avatar
小康2022 已提交
207
### Automatically adapt to DPI/自动适应DPI
小康2022's avatar
小康2022 已提交
208

小康2022's avatar
小康2022 已提交
209
[DPI 级别设置函数](#DPI)
小康2022's avatar
小康2022 已提交
210

小康2022's avatar
小康2022 已提交
211
### Detailed type hints/详细的类型提示
小康2022's avatar
小康2022 已提交
212

小康2022's avatar
小康2022 已提交
213 214 215
参考 [PEP 526](https://peps.python.org/pep-0526/)[PEP 586](https://peps.python.org/pep-0586/)[PEP 604](https://peps.python.org/pep-0604/)[PEP 612](https://peps.python.org/pep-0612/),我采用了最兼容的方式去实现详细的类型提示,可适用 IDE 有 VScode、Pycharm 等。  
那什么是类型提示呢?话不多说,直接看图就行:

小康2022 已提交
216
![type_hint.png](readme_res/type_hint_vscode.png)
小康2022's avatar
小康2022 已提交
217 218 219 220 221

在 VSCode 编辑器中,当鼠标移至类或者函数的名字上面时,会自动显示该类或者函数的注释文档。通过这种方式,不需要看太多的帮助文档和资料就能熟练地使用 tkintertools 模块!

### Across Platforms/跨平台

小康2022 已提交
222
[test.py](test.py) 在 Windows 系统(**Windows10**)上运行的界面如下:
小康2022's avatar
小康2022 已提交
223

小康2022 已提交
224
![test_windows10.png](readme_res/test_windows10.png)
小康2022's avatar
小康2022 已提交
225

小康2022 已提交
226
[test.py](test.py) 在 Linux 系统(**Ubuntu22.04**)上运行的界面如下:
小康2022's avatar
小康2022 已提交
227

小康2022 已提交
228
![test_linux.png](readme_res/test_linux.png)
小康2022's avatar
小康2022 已提交
229

小康2022 已提交
230 231
[test.py](test.py) 在 Windows 系统(**Windows11**)上运行的界面如下(智能控制圆角半径):

小康2022 已提交
232 233 234 235 236
![test_windows11.png](readme_res/test_windows11.png)

### 3D Drawing/3D绘图

[News/最新功能](#news)
小康2022 已提交
237

小康2022's avatar
小康2022 已提交
238
Contents/模块内容
小康2022's avatar
小康2022 已提交
239
-------------------
小康2022's avatar
小康2022 已提交
240

小康2022's avatar
小康2022 已提交
241 242 243
Each non internal class and function in the module will be described in detail here  
这里会详细说明模块中的每个非内部类和函数

小康2022's avatar
小康2022 已提交
244
### Container Widget/容器控件
小康2022's avatar
小康2022 已提交
245

小康2022's avatar
小康2022 已提交
246
1. `Tk`: 窗口类
小康2022's avatar
小康2022 已提交
247

小康2022's avatar
小康2022 已提交
248
    继承于`tkinter.Tk`,在继承了`tkinter`模块内`Tk`的基础上,又加入了对`tkintertools`模块中的`Canvas`对象的支持,并加入了检测窗口大小是否缩放的机制,以使得其子`Canvas`均能正确地进行缩放
小康2022's avatar
小康2022 已提交
249

小康2022's avatar
小康2022 已提交
250
2. `Toplevel`: 顶级窗口类
小康2022's avatar
小康2022 已提交
251

小康2022's avatar
小康2022 已提交
252
    继承于`tkinter.Toplevel`和`Tk`,加入了对`tkintertools`模块中的`Canvas`对象的支持,其余均与`Tk`一样
小康2022's avatar
小康2022 已提交
253

小康2022's avatar
小康2022 已提交
254
3. `Canvas`: 画布类
小康2022's avatar
小康2022 已提交
255

小康2022's avatar
小康2022 已提交
256
    继承于`tkinter.Canvas`,加入了对画布虚拟控件的支持,同时是各类响应事件、缩放控制的管理者,也对`tkinter.Canvas`的实例方法有一定的兼容性
小康2022's avatar
小康2022 已提交
257

小康2022's avatar
小康2022 已提交
258
### Virtual Canvas Widget/虚拟画布控件
小康2022's avatar
小康2022 已提交
259

小康2022's avatar
小康2022 已提交
260
1. `Label`: 标签控件
小康2022's avatar
小康2022 已提交
261

小康2022's avatar
小康2022 已提交
262 263
    标签控件的功能和`tkinter.Label`的功能类似,但更加的多元化  
    下面是`Label`控件的外观:  
小康2022's avatar
小康2022 已提交
264

小康2022 已提交
265
    ![LabelTest.png](readme_res/LabelTest.png)
小康2022's avatar
小康2022 已提交
266

小康2022's avatar
小康2022 已提交
267
    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
268

小康2022's avatar
小康2022 已提交
269
    ```python
小康2022's avatar
小康2022 已提交
270 271 272
    import tkintertools as tkt

    root = tkt.Tk('LabelTest', 1000, 400)
小康2022's avatar
小康2022 已提交
273
    canvas = tkt.Canvas(root, 1000, 400, 0, 0)
小康2022's avatar
小康2022 已提交
274 275


小康2022's avatar
小康2022 已提交
276
    def colorful(x, y, width, height):  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
277 278 279 280 281 282 283
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)


    colorful(510, 175, 480, 150)
小康2022's avatar
小康2022 已提交
284 285 286 287 288
    tkt.Label(canvas, 50, 50, 400, 100, text='NormalLabel\nHere is the text')
    tkt.Label(canvas, 50, 200, 400, 100, radius=20, text='RoundCornerLabel')
    tkt.Label(canvas, 550, 50, 400, 100, text='DisableLabel').set_live(False)
    tkt.Label(canvas, 550, 200, 400, 100, radius=20,
            text='TransparentLabel', color_fill=tkt.COLOR_NONE)
小康2022's avatar
小康2022 已提交
289 290 291

    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
292

小康2022's avatar
小康2022 已提交
293
    </details>
小康2022's avatar
小康2022 已提交
294

小康2022's avatar
小康2022 已提交
295
2. `Button`: 按钮控件
小康2022's avatar
小康2022 已提交
296

小康2022's avatar
小康2022 已提交
297
    按钮控件相较于`tkinter.Button`,其自由度更高,`tkinter.Button`只有在按下的时候才能触发绑定的关联事件,而`Button`却可以在鼠标移至按钮上方时、鼠标按下时、鼠标松开时都可以绑定关联事件  
小康2022's avatar
小康2022 已提交
298 299
    下面是`Button`控件的外观:

小康2022 已提交
300
    ![ButtonTest.png](readme_res/ButtonTest.png)
小康2022's avatar
小康2022 已提交
301 302

    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
303

小康2022's avatar
小康2022 已提交
304
    ```python
小康2022's avatar
小康2022 已提交
305 306 307
    import tkintertools as tkt

    root = tkt.Tk('ButtonTest', 500, 500)
小康2022's avatar
小康2022 已提交
308
    canvas = tkt.Canvas(root, 500, 500, 0, 0)
小康2022's avatar
小康2022 已提交
309 310


小康2022's avatar
小康2022 已提交
311
    def colorful(x, y, width, height):  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
312 313 314 315 316 317 318
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)


    colorful(50, 280, 400, 100)
小康2022's avatar
小康2022 已提交
319 320 321 322 323
    tkt.Button(canvas, 150, 135, 200, 50, text='NormalButton')
    tkt.Button(canvas, 100, 195, 300, 50, radius=10, text='RoundCornerButton')
    tkt.Button(canvas, 150, 255, 200, 50, text='DisableButton').set_live(False)
    tkt.Button(canvas, 100, 315, 300, 50, radius=10,
            text='TransparentButton', color_fill=tkt.COLOR_NONE)
小康2022's avatar
小康2022 已提交
324 325 326

    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
327

小康2022's avatar
小康2022 已提交
328 329 330 331 332 333
    </details>

3. `CheckButton`: 复选框控件

    复选框控件相对于`tkinter`原生的`tkinter.CheckButton`在使用方面更加地简单,同时颜值也上升了不少  
    下面是`CheckButton`控件的外观:
小康2022's avatar
小康2022 已提交
334

小康2022 已提交
335
    ![CheckButtonTest.png](readme_res/CheckButtonTest.png)
小康2022's avatar
小康2022 已提交
336 337 338 339 340 341 342

    <details><summary><b>源代码</b></summary>

    ```python
    import tkintertools as tkt

    root = tkt.Tk('CheckButtonTest', 500, 300)
小康2022's avatar
小康2022 已提交
343
    canvas = tkt.Canvas(root, 500, 300, 0, 0)
小康2022's avatar
小康2022 已提交
344 345 346 347 348 349 350


    def colorful(x, y, width, height):  # type: (int, int, int, int) -> None
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)
小康2022's avatar
小康2022 已提交
351 352


小康2022's avatar
小康2022 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366
    colorful(40, 190, 420, 50)
    tkt.CheckButton(canvas, 50, 50, 30, text='NormalCheckButton', value=True)
    tkt.CheckButton(canvas, 50, 100, 30, text='DisableCheckButton',
                    value=True).set_live(False)
    tkt.CheckButton(canvas, 50, 150, 30, radius=10, text='RoundCornerCheckButton')
    tkt.CheckButton(canvas, 50, 200, 30, radius=15,
                    text='TransparentCheckButton', color_fill=tkt.COLOR_NONE)

    root.mainloop()
    ```

    </details>

4. `Entry`: 输入框控件
小康2022's avatar
小康2022 已提交
367

小康2022's avatar
小康2022 已提交
368
    输入框控件可以轻松地设置输入的文本位置(靠左、居中和靠右),同时,它可以在鼠标移至输入框上方、鼠标未在输入框上方两种状态显示不同的默认文本  
小康2022's avatar
小康2022 已提交
369 370
    下面是`Entry`控件的外观:

小康2022 已提交
371
    ![EntryTest.png](readme_res/EntryTest.png)
小康2022's avatar
小康2022 已提交
372
    
小康2022's avatar
小康2022 已提交
373
    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
374

小康2022's avatar
小康2022 已提交
375
    ```python
小康2022's avatar
小康2022 已提交
376 377 378
    import tkintertools as tkt

    root = tkt.Tk('EntryTest', 500, 400)
小康2022's avatar
小康2022 已提交
379
    canvas = tkt.Canvas(root, 500, 400, 0, 0)
小康2022's avatar
小康2022 已提交
380 381


小康2022's avatar
小康2022 已提交
382
    def colorful(x, y, width, height):  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
383 384 385 386 387 388 389
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)


    colorful(50, 193, 400, 100)
小康2022's avatar
小康2022 已提交
390 391 392 393 394 395 396 397 398 399 400
    tkt.Entry(canvas, 20, 20, 200, 30, text=('LeftEntry', 'Enter'))
    tkt.Entry(canvas, 20, 55, 200, 30, text=(
        'CenterEntry', 'Enter'), justify='center')
    tkt.Entry(canvas, 20, 90, 200, 30, text=(
        'RightEntry', 'Enter'), justify='right')
    tkt.Entry(canvas, 270, 20, 200, 30, radius=8, text='LeftEntry')
    tkt.Entry(canvas, 270, 55, 200, 30, radius=8,
            text='CenterEntry', justify='center')
    tkt.Entry(canvas, 270, 90, 200, 30, radius=8,
            text='RightEntry', justify='right')
    tkt.Entry(canvas, 100, 150, 300, 35, text=('PasswordEntry',
小康2022's avatar
小康2022 已提交
401
            'Click To Enter'), justify='center', show='●')
小康2022's avatar
小康2022 已提交
402
    tkt.Entry(canvas, 100, 200, 300, 35, text='DisableEntry',
小康2022's avatar
小康2022 已提交
403
            justify='center').set_live(False)
小康2022's avatar
小康2022 已提交
404
    tkt.Entry(canvas, 100, 250, 300, 35, text='TransparentEntry',
小康2022's avatar
小康2022 已提交
405 406 407 408
            justify='center', color_fill=tkt.COLOR_NONE)

    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
409

小康2022's avatar
小康2022 已提交
410 411 412
    </details>

5. `Text`: 文本框控件
小康2022's avatar
小康2022 已提交
413

小康2022's avatar
小康2022 已提交
414
    文本框类似于输入框,这里就不再赘述  
小康2022's avatar
小康2022 已提交
415 416
    下面是`Text`控件的外观:

小康2022 已提交
417
    ![TextTest.png](readme_res/TextTest.png)
小康2022's avatar
小康2022 已提交
418 419
    
    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
420 421 422 423 424

    ```python
    import tkintertools as tkt

    root = tkt.Tk('TextTest', 1000, 400)
小康2022's avatar
小康2022 已提交
425
    canvas = tkt.Canvas(root, 1000, 400, 0, 0)
小康2022's avatar
小康2022 已提交
426 427


小康2022's avatar
小康2022 已提交
428
    def colorful(x, y, width, height) -> None:  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
429 430 431 432 433 434 435
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)


    colorful(510, 175, 480, 150)
小康2022's avatar
小康2022 已提交
436 437 438 439 440 441
    tkt.Text(canvas, 50, 50, 400, 100, text=('NormalText(Left)', 'Click To Enter'))
    tkt.Text(canvas, 50, 200, 400, 100, radius=20,
            text='RoundCornerText(Center)', justify='center')
    tkt.Text(canvas, 550, 50, 400, 100, text='DisableText').set_live(False)
    tkt.Text(canvas, 550, 200, 400, 100, radius=20,
            text='TransparentText(Right)', justify='right', color_fill=tkt.COLOR_NONE)
小康2022's avatar
小康2022 已提交
442 443 444

    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
445

小康2022's avatar
小康2022 已提交
446
    </details>
小康2022's avatar
小康2022 已提交
447

小康2022's avatar
小康2022 已提交
448
6. `Progressbar`: 进度条控件
小康2022's avatar
小康2022 已提交
449

小康2022's avatar
小康2022 已提交
450
    进度条控件相比`tkinter.ttk.Progressbar`,外观上的自由度较大  
小康2022's avatar
小康2022 已提交
451 452
    下面是`Progressbar`控件的外观:

小康2022 已提交
453
    ![ProgressbarTest.png](readme_res/ProgressbarTest.png)
小康2022's avatar
小康2022 已提交
454 455

    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
456

小康2022's avatar
小康2022 已提交
457
    ```python
小康2022's avatar
小康2022 已提交
458 459 460
    import tkintertools as tkt

    root = tkt.Tk('ProgressbarTest', 500, 500)
小康2022's avatar
小康2022 已提交
461
    canvas = tkt.Canvas(root, 500, 500, 0, 0)
小康2022's avatar
小康2022 已提交
462 463


小康2022's avatar
小康2022 已提交
464
    def colorful(x, y, width, height) -> None:  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
465 466 467 468 469 470 471 472 473
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#0000FF'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)


    colorful(30, 290, 440, 50)
    tkt.Progressbar(canvas, 50, 50, 400, 30)
    tkt.Progressbar(canvas, 50, 100, 400, 30).load(.6667)
小康2022's avatar
小康2022 已提交
474
    tkt.Progressbar(canvas, 50, 150, 400, 30, borderwidth=5).load(1)
小康2022's avatar
小康2022 已提交
475 476 477 478 479 480 481 482 483 484
    (_ := tkt.Progressbar(canvas, 50, 200, 400, 30)).load(0.3333)
    _.set_live(False)
    tkt.Progressbar(canvas, 50, 250, 400, 30, color_bar=(
        'lightyellow', 'skyblue')).load(.5)
    tkt.Progressbar(canvas, 50, 300, 400, 30, color_bar=('', 'orange')).load(.1667)

    progressbar = tkt.Progressbar(canvas, 50, 375, 400, 30)
    progressbar_2 = tkt.Progressbar(canvas, 50, 425, 400, 30)


小康2022's avatar
小康2022 已提交
485
    def load(total, count=0):  # type: (int, int) -> None
小康2022's avatar
小康2022 已提交
486 487 488 489 490 491 492 493 494 495
        """ load progressbar """
        progressbar.load(count/total)
        progressbar_2.load(1-count/total)
        if count < total:
            root.after(3, load, total, count+1)


    load(10000)
    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
496

小康2022's avatar
小康2022 已提交
497
    </details>
小康2022's avatar
小康2022 已提交
498

小康2022's avatar
小康2022 已提交
499
### Tool Class/工具类
小康2022's avatar
小康2022 已提交
500

小康2022's avatar
小康2022 已提交
501
1. `PhotoImage`: 图片类
小康2022's avatar
小康2022 已提交
502

小康2022's avatar
小康2022 已提交
503 504 505
    `PhotoImage`类继承于`tkinter.PhotoImage`,它是在`tkinter.PhotoImage`的基础上做功能的强化,对 gif 动图有很好的支持,仅需极少量代码即可实现动图的显示,还可以设置动图显示的速度,此外,对 png 类型的图片的支持也有强化,可以在不依赖任何第三方模块或者库的情况下,对 png 图片进行缩放

2. `Singleton`: 单例模式类
小康2022's avatar
小康2022 已提交
506 507

    单例模式,不用介绍了吧?通过继承它来使用
小康2022's avatar
小康2022 已提交
508

小康2022's avatar
小康2022 已提交
509
### Tool Function/工具函数
小康2022's avatar
小康2022 已提交
510

小康2022's avatar
小康2022 已提交
511
1. `move`: <a name="move">移动函数</a>
小康2022's avatar
小康2022 已提交
512

小康2022's avatar
小康2022 已提交
513 514
    移动函数可以轻松地按一定的规律、移动速度、移动时间去移动`tkintertools`模块内的所有对象,同时兼容了`tkinter`内的对象,即`tkinter`中的对象也可以很方便地移动,甚至它还可以移动窗口的位置!

小康2022 已提交
515
    ![MoveTest.gif](readme_res/MoveTest.gif)
小康2022's avatar
小康2022 已提交
516
    
小康2022's avatar
小康2022 已提交
517
    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
518

小康2022's avatar
小康2022 已提交
519
    ```python
小康2022's avatar
小康2022 已提交
520 521 522
    import tkintertools as tkt

    root = tkt.Tk('MoveTest', 500, 500)
小康2022's avatar
小康2022 已提交
523
    canvas = tkt.Canvas(root, 500, 500, 0, 0)
小康2022's avatar
小康2022 已提交
524 525 526
    rect = canvas.create_rectangle(50, 350, 150, 450)


小康2022's avatar
小康2022 已提交
527 528
    def move_window(switch=[True]):  # type: (list[bool]) -> None
        tkt.move(root, None, 1000 if switch[0] else -1000, 0, 800, mode='flat')
小康2022's avatar
小康2022 已提交
529 530 531
        switch[0] = not switch[0]


小康2022's avatar
小康2022 已提交
532 533 534
    def move_button(switch=[True]) -> None:  # type: (list[bool]) -> None
        tkt.move(canvas, button, 200 if switch[0]
                else -200, 0, 500, mode='rebound')
小康2022's avatar
小康2022 已提交
535 536 537
        switch[0] = not switch[0]


小康2022's avatar
小康2022 已提交
538 539
    def move_rect(switch=[True]):  # type: (list[bool]) -> None
        tkt.move(canvas, rect, 200 if switch[0] else -200, 0, 500, mode='smooth')
小康2022's avatar
小康2022 已提交
540 541 542
        switch[0] = not switch[0]


小康2022's avatar
小康2022 已提交
543 544 545 546 547 548
    tkt.Button(canvas, 50, 50, 200, 40, radius=10,
            text='MoveWindow', command=move_window)
    tkt.Button(canvas, 50, 100, 200, 40, radius=10,
            text='MoveRect', command=move_rect)
    button = tkt.Button(canvas, 50, 150, 200, 40, radius=10,
                        text='MoveButton', command=move_button)
小康2022's avatar
小康2022 已提交
549 550 551

    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
552

小康2022's avatar
小康2022 已提交
553 554 555
    </details>

2. `text`: 文本函数
小康2022's avatar
小康2022 已提交
556 557

    可以快速并方便地得到一个参数长度的字符串,且字符串的内容可以指定位置  
小康2022's avatar
小康2022 已提交
558
    如:得到一个 20 长度的字符串 “tkintertools”  
小康2022's avatar
小康2022 已提交
559
    <pre>
小康2022's avatar
小康2022 已提交
560 561
    `left`   : "tkintertools        "  
    `center` : "    tkintertools    "  
小康2022's avatar
小康2022 已提交
562
    `right`  : "        tkintertools"</pre>
小康2022's avatar
小康2022 已提交
563

小康2022's avatar
小康2022 已提交
564
3. `color`: <a name="Gradient">颜色函数</a>
小康2022's avatar
小康2022 已提交
565 566

    颜色函数可以轻松求出一个颜色到另外一个颜色的过渡颜色,因此可以轻松得到渐变色的效果,同时,改变传入的参数还可以得到传入颜色的对比色  
小康2022's avatar
小康2022 已提交
567 568
    第二张图是 test.py 在图像测试中绘制的图案

小康2022 已提交
569
    ![ColorTest.png](readme_res/ColorTest.png)
小康2022's avatar
小康2022 已提交
570

小康2022 已提交
571
    ![Test_Draw.png](readme_res/Test_Draw.png)
小康2022's avatar
小康2022 已提交
572

小康2022's avatar
小康2022 已提交
573
    <details><summary><b>源代码</b></summary>
小康2022's avatar
小康2022 已提交
574

小康2022's avatar
小康2022 已提交
575
    ```python
小康2022's avatar
小康2022 已提交
576 577 578
    import tkintertools as tkt

    root = tkt.Tk('ColorTest', 500, 500)
小康2022's avatar
小康2022 已提交
579
    canvas = tkt.Canvas(root, 500, 500, 0, 0)
小康2022's avatar
小康2022 已提交
580 581


小康2022's avatar
小康2022 已提交
582
    def colorful(x, y, width, height):  # type: (int, int, int, int) -> None
小康2022's avatar
小康2022 已提交
583 584 585 586 587 588 589 590 591 592 593 594
        """ Gradient colors """
        for i in range(width):
            color = tkt.color(('#FF0000', '#00FF00'), i/width)
            color_2 = tkt.color(('#FFFFFF', '#000000'), i/width)
            canvas.create_line(x+i, y, x+i, y+height, fill=color)
            canvas.create_oval(250-i/3, 300-i/3, 250+i/3, 300 +
                            i/3, outline=color_2, width=2)


    colorful(50, 50, 400, 100)
    root.mainloop()
    ```
小康2022's avatar
小康2022 已提交
595

小康2022's avatar
小康2022 已提交
596 597 598
    </details>

4. `askfont`: 字体选择对话框
小康2022's avatar
小康2022 已提交
599

小康2022's avatar
小康2022 已提交
600
    `askfont`函数可以打开默认的字体选择窗口,这个窗口虽然是默认的,但它实际上无法在`tkinter`中打开,因为`tkinter`并没有对应的 API 能够做到这一点。但是,`tkintertools`调用并封装了原生的 tcl 的命令,使得字体选择框能够被我们使用。
小康2022's avatar
小康2022 已提交
601

小康2022 已提交
602
    ![font.png](readme_res/font.png)
小康2022's avatar
小康2022 已提交
603

小康2022's avatar
小康2022 已提交
604
5. `SetProcessDpiAwareness`: <a name="DPI">DPI 级别设置函数</a>
小康2022's avatar
小康2022 已提交
605

小康2022's avatar
小康2022 已提交
606
    这个函数实际上只是对函数`ctypes.WinDLL('shcore').SetProcessDpiAwareness`的一个简单包装,其值可为 0、1 和 2,分别代表程序 DPI 的不同级别,那么缩放效果也就不同,`tkintertools`选择的值是 1,但程序默认值实际为 0  
小康2022's avatar
小康2022 已提交
607 608
    下面是未执行这个函数的效果
    
小康2022 已提交
609
    ![SetProcessDpiAwareness_0.png](readme_res/SetProcessDpiAwareness_0.png)
小康2022's avatar
小康2022 已提交
610 611 612

    <p>下面是执行了这个函数的效果</p>

小康2022 已提交
613
    ![SetProcessDpiAwareness_1.png](readme_res/SetProcessDpiAwareness_1.png)
小康2022's avatar
小康2022 已提交
614

小康2022's avatar
小康2022 已提交
615
    从上面的两张图中可以很明显的看出第一张很模糊,第二张很清晰,这就是 DPI 级别不同的原因,不过这一点在屏幕缩放比不是 100% 的时候才会出现  
小康2022's avatar
小康2022 已提交
616
    大家对上面的图肯定很熟悉,这不就是 IDLE 吗!?对,这个的问题的解决办法也是来自于 IDLE 的源代码 [pyshell.py line 18~20]  
小康2022's avatar
小康2022 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    注意:该函数在程序的不同位置执行的效果不一样!一般用在`mainloop`之前,但`tkintertools`已经在`mainloop`函数中嵌入了该函数,无需再设置一次 DPI 级别,此函数是为了原生`tkinter`程序用的。

Examples/实战示例
----------------

以下三个为使用了 tkintertools 的典型案例,供大家参考,程序均免费,源代码开放!  
前面两个是我一边改进 tkintertools 模块,一边写的实战,有一定的 bug,但不影响正常使用。  
只有把模块真正地用到实战上去,才知道哪有 bug,哪里还需要改进!

### 任务清单小工具

* 文章链接: https://xiaokang2022.blog.csdn.net/article/details/128561339
* 代码仓库: https://gitcode.net/weixin_62651706/todolist
* 程序下载(含打包好的程序和源代码): https://wwc.lanzoum.com/iyxL30kpkcbe
* 推荐指数: 👍

这个案例使用了 tkintertools-v2.5.7 版本(新版已无法兼容),含有一些 bug,大量采用了 tkintertools 的控件,取得了比较好的界面效果。体现了 tkintertools 模块与 tkinter 模块相比在颜值上的碾压性!

小康2022 已提交
635 636 637
![todolist.gif](https://img-blog.csdnimg.cn/img_convert/dc1a598c3f082253c1ebc7bbca0b98ce.gif)

![todolist.gif](https://img-blog.csdnimg.cn/img_convert/7f34451deda1af13712a9edcb37f20b4.gif)
小康2022's avatar
小康2022 已提交
638 639 640 641 642 643 644 645 646 647 648

### 中国象棋游戏

* 文章链接: https://xiaokang2022.blog.csdn.net/article/details/128852029
* 代码仓库: https://gitcode.net/weixin_62651706/chess
* 程序下载(含打包好的程序和源代码): https://wwc.lanzoum.com/iwgp00mlewpa
* 推荐指数: 👍👍

注意:源代码有解压密码,解压密码在链接文章中,请仔细查找!  
这个案例使用了 tkintertools-v2.5.9.5 版本(新版已无法兼容), 含有少量 bug,部分 UI 采用了 tkintertools,部分 UI 采用了 tkinter,属于混合使用。体现了 tkintertools 模块对 tkinter 模块的兼容性!

小康2022 已提交
649 650 651
![chess.png](https://img-blog.csdnimg.cn/43df0568d4b34078a443a098b67c126a.png)

![chess.png](https://img-blog.csdnimg.cn/fc768093715d47d7b14bea015a921e3d.png)
小康2022's avatar
小康2022 已提交
652 653 654 655 656 657 658 659 660 661

### 简易登录界面

* 文章链接: 暂无
* 代码仓库: https://gitcode.net/weixin_62651706/tester
* 程序下载: 暂无
* 推荐指数: 👍👍👍

这个案例使用了最新稳定版的 tkintertools-v2.6.0,界面非常稳定,几乎没有 bug,完全采用 tkintertools 的控件,颜值很高,界面非常流畅。体现了 tkintertools 模块与 tkinter 模块相比在性能上的优越性!

小康2022 已提交
662 663 664
![exam3_1.png](readme_res/exam3_1.png)

![exam3_2.png](readme_res/exam3_2.png)
小康2022's avatar
小康2022 已提交
665

小康2022's avatar
小康2022 已提交
666 667
More/更多
---------
小康2022's avatar
小康2022 已提交
668

小康2022 已提交
669 670
> GitHub:  
> https://github.com/Xiaokang2022/tkintertools
小康2022's avatar
小康2022 已提交
671

小康2022 已提交
672 673
> Gitee(Mirror/镜像):  
> https://gitee.com/xiaokang-2022/tkintertools
小康2022's avatar
小康2022 已提交
674

小康2022 已提交
675 676 677 678
> GitCode(Mirror/镜像):  
> https://gitcode.net/weixin_62651706/tkintertools

还有更多内容请在 [源代码](tkintertools/) 中探索!