一、- Tkinter 编程
Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
由于 Tkinter 是内置到 python 的安装包中、只要安装好 Python 之后就能 import Tkinter 库、而且 IDLE 也是用 Tkinter 编写而成、对于简单的图形界面 Tkinter 还是能应付自如。
二、组件

三、窗口
1、新建窗口
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter') #窗口标题
window.iconbitmap('bi.ico') #窗口图片
window.geometry('900x760')
window.resizable(width=False, height=True)
window.config(bg="#eeeeee")
window.mainloop()
2、居中
通过设置 window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2)) ,可对窗口进行居中设置
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter') #窗口标题
window.iconbitmap('bi.ico') #窗口图片
height= window.winfo_screenheight() #获取屏幕高度
width= window.winfo_screenwidth() #获取屏幕宽度
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.resizable(width=False, height=True) #设置宽度不变
window.config(bg="#eeeeee") #窗口背颜色
window.mainloop()
3、窗口最小缩放和最大缩放
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
window.iconbitmap('bi.ico')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.resizable(width=False, height=True)
window.config(bg="#eeeeee")
window.minsize(100,100) #窗口的最小缩放
window.maxsize(1000,800)#窗口最大缩放
window.mainloop()
4、设置窗口透明度
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
window.iconbitmap('bi.ico')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.resizable(width=False, height=True)
window.config(bg="#eeeeee")
window.minsize(100,100)
window.maxsize(1000,800)
window.attributes("-alpha",0.9) #设置窗口的透明度,1为不透明,0为完全透明
window.mainloop()
5、设置窗口置顶模式,该窗口将永远置于顶层
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
window.attributes("-topmost",1) #设为窗口置顶模式
window.mainloop()
attributes(*args)
-- 设置和获取窗口属性
-- 如果你只给出选项名,将返回当前窗口该选项的值
-- 注意:以下选项不支持关键字参数,你需要在选项前添加横杠(-)并用字符串的方式表示,用逗号(,)隔开选项和值。
-- 例如你希望设置窗口的透明度为 50%,你应该使用 attribute("-alpha", 0.5) 代替 attribute(alpha=0.5)
-- 下方表格列举了 args 可以使用各个选项的具体含义及用法:
选项 含义
alpha 1.(Windows,Mac)控制窗口的透明度
- 1.0 表示不透明,0.0 表示完全透明
- 该选项并不支持所有的系统,对于不支持的系统,Tkinter 绘制一个不透明(1.0)的窗口
disabled (Windows)禁用整个窗口(这时候你只能从任务管理器中关闭它)
fullscreen (Windows,Mac)如果设置为 True,则全屏显示窗口
modified (Mac)如果设置为 True,该窗口被标记为改动过
titlepath (Mac)设置窗口代理图标的路径
toolwindow (Windows)如果设置为 True,该窗口采用工具窗口的样式
topmost (Windows,Mac)如果设置为 True,该窗口将永远置于顶层
6、窗口最大化、最小化
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
window.iconify()#窗口最小化
window.state("zoomed")#启动窗口最大化
window.mainloop()
7、显示窗口
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
window.iconify()#窗口最小化
window.deiconify()#显示窗口,iconify最小化后可以使用deiconify进行显示
window.mainloop()
8、全屏
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
window.attributes("-fullscreen",1) #全屏
window.mainloop()
9、返回X窗口的标示符或者是当前窗口的句柄
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
print(window.frame()) #返回X窗口的标示符或者是当前窗口的句柄
window.mainloop()
10、grid
grid(baseWidth=None, baseHeight=None, widthInc=None, heightInc=None)
-- 通知窗口管理器该窗口将以网格的形式重新调整尺寸
-- baseWidth 和 baseHeight 指定 Tk_GeometryRequest 要求的网格单元数
-- widthInc 和 heightInc 指定网格单元的宽度和高度(像素
import tkinter as tk
# 建立窗口
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('800x600+%d+%d'%((width-800)/2,(height-600)/2))
window.config(bg="#eeeeee")
window.grid(baseWidth=100, baseHeight=100, widthInc=4, heightInc=4)
window.mainloop()
网友评论