This blog intrudoce how to use Tkinter GUI in the python.
这篇Blog主要深入 Tkinter 的用法
Tkinter:
Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's de facto standard GUI. Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python. The nameTkinter comes from Tk interface.
Tk 是一个开源的工具 用以 开发 GUI
Tk 有很多小工具交互 广泛的应用于 不同的主机应用, 比如 按钮 ,菜单, 窗口 ,输入
早在 90 年代, tk 的前身 就在 脚本语言 出现过 : tool command language (TCL)
Tkinter相关就不加以阐述 直接进入安装部分
Target: 目标
--- 初用---
- 1. 用4行code 建造一个可用的 GUI
- 2. 自定义 GUI 的 大小
- 3. 调控GUI 参数
——————————————————————————————————————————————初用
1. 导入
创造 GUI
#======================
# imports
#======================
import tkinter as tk # Python 3: "t" lower-case
# Create instance
win = tk.Tk()
# Add a title
win.title("Python GUI")
#======================
# Start GUI
#======================
win.mainloop()
结果如下:
2. 修改默认大小
#======================
# imports
#======================
import tkinter as tk
# Create instance
win = tk.Tk()
# Add a title
win.title("Python GUI")
#======================
# functions
#======================
def get_current_windows_size():
win.update() # to get runtime size
print('width =', win.winfo_width())
print('height =', win.winfo_height())
def increase_window_width():
# min width can not be resized to less
# default heigth can be
win.minsize(width=300, height=1) # 1 = default
#======================
# Start GUI
#======================
get_current_windows_size()
increase_window_width()
print()
get_current_windows_size()
win.mainloop()
-
win.update() 更新其他方程对这个window 做过的更改
-
minsize(w, h) 限制窗口最低尺寸 / 1 在这里面指默认
-
mainloop() 运行, 因为窗口可被用于执行动画 因此是在不停的执行 刷新的
这个概念会在TKinter —— GUI in python 4. 小组件 赋功能 中解释
3. 预防不被更改
生成的窗口不能用箭头 拖动去扩大或缩小
def increase_window_width():
# min width can not be resized to less
# default height can be
win.minsize(width=300, height=1) # 1 = default
# disable resizing the GUI
win.resizable(0,0)
异常简单的初始化 就完成了!!
更多的内容请移步 下一个教程