-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkutils.py
95 lines (70 loc) · 2.31 KB
/
tkutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding: utf-8
import tkinter as tk
import tkinter.font as tkFont
from PIL import Image, ImageTk
from tkinter import messagebox
def show_info(message=""):
messagebox.showinfo("提示框", message)
def show_confirm(message=""):
"""
True : yes
False : no
"""
return messagebox.askyesno("确认框", message)
def center_window(win, width=None, height=None):
""" 将窗口屏幕居中 """
screenwidth = win.winfo_screenwidth()
screenheight = win.winfo_screenheight()
if width is None:
width, height = get_window_size(win)[:2]
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/3)
win.geometry(size)
def get_window_size(win, update=True):
""" 获得窗体的尺寸 """
if update:
win.update()
return win.winfo_width(), win.winfo_height(), win.winfo_x(), win.winfo_y()
def tkimg_resized(img, w_box, h_box, keep_ratio=True):
"""对图片进行按比例缩放处理"""
w, h = img.size
if keep_ratio:
if w > h:
width = w_box
height = int(h_box * (1.0 * h / w))
if h >= w:
height = h_box
width = int(w_box * (1.0 * w / h))
else:
width = w_box
height = h_box
img1 = img.resize((width, height), Image.ANTIALIAS)
tkimg = ImageTk.PhotoImage(img1)
return tkimg
def image_label(frame, img, width, height, keep_ratio=True):
"""输入图片信息,及尺寸,返回界面组件"""
if isinstance(img, str):
_img = Image.open(img)
else:
_img = img
lbl_image = tk.Label(frame, width=width, height=height)
tk_img = tkimg_resized(_img, width, height, keep_ratio)
lbl_image.image = tk_img
lbl_image.config(image=tk_img)
return lbl_image
def _font(fname="微软雅黑", size=12, bold=tkFont.NORMAL):
"""设置字体"""
ft = tkFont.Font(family=fname, size=size, weight=bold)
return ft
def _ft(size=12, bold=False):
"""极简字体设置函数"""
if bold:
return _font(size=size, bold=tkFont.BOLD)
else:
return _font(size=size, bold=tkFont.NORMAL)
def h_seperator(parent, height=2): # height 单位为像素值
"""水平分割线, 水平填充 """
tk.Frame(parent, height=height, bg="whitesmoke").pack(fill=tk.X)
def v_seperator(parent, width, bg="whitesmoke"): # width 单位为像素值
"""垂直分割线 , fill=tk.Y, 但如何定位不确定,直接返回对象,由容器决定 """
frame = tk.Frame(parent, width=width, bg=bg)
return frame