|
楼主 |
发表于 2022-3-18 14:36:34
|
显示全部楼层
import tkinter as tk
from tkinter import scrolledtext
from tkinter import ttk
import math
from tkinter import messagebox
win =tk.Tk()
# 设置主窗口
win.title("色差公式")
win.geometry('1068x681+100+50')
def count():
#获取输入样品的L* a* b*,通过float()方法将str类型转为float类型
try:
#以标样数据 L=44.28 a=27.49 b=-0.17
#试样数据 L=42.94 a=33.35 b=-2.21
Lstd = float(entry1.get())
astd = float(entry2.get())
bstd = float(entry3.get())
except:
messagebox.showinfo(title='提示',message='请输入标样信息')
#循环获取scrolledtext中的内容
try:
fetched_content = [list(map(float, i.split())) for i in scr1.get('1.0', 'end-1c').splitlines()]
fetched_content = list(map(list, zip(*fetched_content)))
Lsp = fetched_content[0]
asp = fetched_content[1]
bsp = fetched_content[2]
except:
messagebox.showinfo(title='提示', message='请输入试样信息')
#获取下拉控件Combobox中的值
if cbox.get() == '请选择色差公式':
messagebox.showinfo(title='提示',message='请选择色差公式')
elif cbox.get() == 'CIELAB':
CIELAB(Lstd,astd,bstd,Lsp,asp,bsp)
elif cbox.get() == 'CMC':
CMC(Lstd,astd,bstd,Lsp,asp,bsp)
elif cbox.get() == 'CIE94':
CIE94(Lstd,astd,bstd,Lsp,asp,bsp)
elif cbox.get() == 'CIE2000':
CIE2000(Lstd,astd,bstd,Lsp,asp,bsp)
def CIELAB(Lstd,astd,bstd,Lsp,asp,bsp):
e = 0
while e < len(Lsp):
#明度差
DL = Lsp[e]-Lstd
print('明度差是:{:.4f}'.format(DL))
#饱和度差
Csp = (asp[e]**2+bsp[e]**2)**(1/2)
Cstd = (astd**2 + bstd**2)**(1/2)
DCs=Csp-Cstd
print('饱和度差是:{:.4f}'.format(DCs))
#色度差
Da=asp[e]-astd
Db=bsp[e]-bstd
DCc=(Da**2+Db**2)**(1/2)
print('色度差是:{:.4f}'.format(DCc))
#色相差
DH=(DCc**2-DCs**2)**(1/2)
print('色相差是:{:.4f}'.format(DH))
#总色差
DE=(DL**2+DCs**2+DH**2)**(1/2)
print('总色差是:{:.4f}'.format(DE))
scr2.insert('end', '{:.4f}'.format(DE))
scr2.insert(tk.INSERT, '\n')
e=e+1
def CMC(Lstd,astd,bstd,Lsp,asp,bsp):
e = 0
while e < len(Lsp):
# 明度差
DL = Lsp[e] - Lstd
print('明度差是:{:.4f}'.format(DL))
# 饱和度差
Csp = (asp[e] ** 2 + bsp[e] ** 2) ** (1 / 2)
Cstd = (astd ** 2 + bstd ** 2) ** (1 / 2)
DCs = Csp - Cstd
print('饱和度差是:{:.4f}'.format(DCs))
# 色度差
Da = asp[e] - astd
Db = bsp[e] - bstd
DCc = (Da ** 2 + Db ** 2) ** (1 / 2)
print('色度差是:{:.4f}'.format(DCc))
# 色相差
DH = (DCc ** 2 - DCs ** 2) ** (1 / 2)
print('色相差是:{:.4f}'.format(DH))
#色相角
hstd=math.atan(bstd/astd)
if hstd>=164 and hstd<345:
t=0.56+abs(0.2*math.cos(hstd+168))
else:
t=0.36+abs(0.4*math.cos(hstd+35))
if Lstd<16:
SL=0.511
else:
SL=(0.040975*Lstd)/(1+0.01765*Lstd)
SC=0.0638*Cstd/(1+0.0131*Cstd)+0.638
f=(Cstd**4/(Cstd**4+1900))**(1/2)
SH=SC*(t*f+1-f)
print('色相角是:{:.4f}'.format(DH))
# 总色差
DE = ((DL/2*SL)**2 + (DCs/SC)**2 + (DH/SH)**2)** (1/2)
print('总色差是:{:.4f}'.format(DE))
scr2.insert('end', '{:.4f}'.format(DE))
scr2.insert(tk.INSERT, '\n')
e = e + 1
def CIE94(Lstd,astd,bstd,Lsp,asp,bsp):
#纺织品色差计算,常数取值
KL=2
KC=1
KH=1
e = 0
while e < len(Lsp):
# 明度差
DL = Lsp[e] - Lstd
print('明度差是:{:.4f}'.format(DL))
# 饱和度差
Csp = (asp[e] ** 2 + bsp[e] ** 2) ** (1 / 2)
Cstd = (astd ** 2 + bstd ** 2) ** (1 / 2)
DCs = Csp - Cstd
print('饱和度差是:{:.4f}'.format(DCs))
# 色度差
Da = asp[e] - astd
Db = bsp[e] - bstd
DCc = (Da ** 2 + Db ** 2) ** (1 / 2)
print('色度差是:{:.4f}'.format(DCc))
# 色相差
DH = (DCc ** 2 - DCs ** 2) ** (1 / 2)
print('色相差是:{:.4f}'.format(DH))
# SL SC SH
Cm = (Cstd*Csp)**(1/2)
SL=1
SC=1+0.045*Cm
SH=1+0.015*Cm
# 总色差
DE = ((DL / 2 * SL) ** 2 + (DCs / SC) ** 2 + (DH / SH) ** 2) ** (1 / 2)
print('总色差是:{:.4f}'.format(DE))
scr2.insert('end', '{:.4f}'.format(DE))
scr2.insert(tk.INSERT, '\n')
e = e + 1
def CIE2000(Lstd,astd,bstd,Lsp,asp,bsp):
e = 0
while e < len(Lsp):
#饱和度
Cssp=(asp[e]**2 + bsp[e]**2)**(1/2)
Csstd=(astd**2 + bstd**2)**(1/2)
#不确定
Cab=(Cssp + Csstd)/2
G=0.5*(1-((Cab**7)/(Cab**7 + 25**7))**(1/2))
Lsp2=Lsp[e]
Lstd2=Lstd
bsp2=bsp[e]
bstd2=bstd
asp2=(1+G)*asp[e]
astd2=(1+G)*astd
Csp2=(asp2**2+bsp2**2)**(1/2)
Cstd2=(astd2**2+bstd2**2)**(1/2)
DC2=Csp2-Cstd2
DL2=Lsp2-Lstd2
if asp2>0 and bsp2>=0:
hsp2=math.atan(bsp2/asp2)*180/math.pi
elif asp2>0 and bsp2<0:
hsp2=math.atan(bsp2/asp2)*180/math.pi + 360
elif asp2<0:
hsp2=math.atan(bsp2/asp2)*180/math.pi +180
elif asp2==0 and bsp2>0:
hsp2=90
elif asp2==0 and bsp2<0:
hsp2=270
elif asp2==0 and bsp2==0:
hsp2=0
if astd2>0 and bstd2>=0:
hstd2=math.atan(bstd2/astd2)*180/math.pi
elif astd2>0 and bstd2<0:
hstd2=math.atan(bstd2/astd2)*180/math.pi+360
elif astd2<0:
hstd2=math.atan(bstd2/astd2)*180/math.pi+180
elif astd2==0 and bstd2>0:
hstd2=90
elif astd2==0 and bstd2<0:
hstd2=270
elif astd2==0 and bstd2==0:
hstd2=0
if Csp2*Cstd2==0:
Dh2=0
elif (Csp2*Cstd2 != 0) and (abs(hsp2-hstd2)<=180):
Dh2=hsp2-hstd2
elif (Csp2*Cstd2 !=0) and ((hsp2-hstd2)>180):
Dh2=hsp2-hstd2-360
elif (Csp2*Cstd2 !=0) and ((hsp2-hstd2)<-180):
Dh2=hsp2-hstd2+360
DH2=2*(Csp2*Cstd2)**(1/2) *math.sin(Dh2/2*math.pi/180)
meanL2=(Lsp2+Lstd2)/2
meanC2=(Csp2+Cstd2)/2
if abs(hsp2-hstd2)<=180 and Csp2*Cstd2!=0:
meanh2=(hsp2+hstd2)/2
elif abs(hsp2-hstd2)>180 and (hsp2+hstd2)<360 and Csp2*Cstd2!=0:
meanh2=(hsp2+hstd2+360)/2
elif Csp2*Cstd2!=0:
meanh2=(hsp2+hstd2)
else:
meanh2=(hsp2+hstd2-360)/2
SL=1+(0.015*(meanL2-50)**2)/(20+(meanL2-50)**2)**(1/2)
SC=1+0.045*meanC2
T=1-0.17*math.cos((meanh2-30)*math.pi/180) + 0.24*math.cos(2*meanh2*math.pi/180)+0.32*math.cos((3*meanh2+6)*math.pi/180)-0.2*math.cos((4*meanh2-63)*math.pi/180)
SH=1+0.015*meanC2*T
RC=2*(((meanC2**7)/(meanC2**7+25**7))**(1/2))
temp=(-((meanh2-275)/25)**2)
oo=30*math.exp(-((meanh2-275)/25)**2)
RT=-math.sin(2*oo*math.pi/180)*RC
KL=2
KC=1
KH=1
DE=((DL2/KL/SL)**2 +(DC2/KC/SC)**2 +(DH2/KH/SH)**2 +RT*(DC2/KC/SC)*(DH2/KH/SH))**(1/2)
print('总色差是:{:.4f}'.format(DE))
scr2.insert('end', '{:.4f}'.format(DE))
scr2.insert(tk.INSERT, '\n')
e=e+1
# 总色差
'''DE = ((DL / 2 * SL) ** 2 + (DCs / SC) ** 2 + (DH / SH) ** 2) ** (1 / 2)
print('总色差是:{:.4f}'.format(DE))
scr2.insert('end', '{:.4f}'.format(DE))
scr2.insert(tk.INSERT, '\n')
e = e + 1'''
print('CIE2000()')
#标样和试样文字
label1=tk.Label(win,text="标样",width=10,font=("隶书", 18)).place(x=160,y=30)
label2=tk.Label(win,text="试样",width=10,font=("隶书", 18)).place(x=580,y=30)
#L* a* b* 位置,使用的时label标签
label3=tk.Label(win,text="L*\ta*\tb*").place(x=150,y=60)
label4=tk.Label(win,text="L*\ta*\tb*").place(x=600,y=60)
#标样的L*,a*,b*的值,使用的是entry标签
#L代表明度值,a,b色度坐标
entry1=tk.Entry(win)
entry1.place(x=130,y=80,width=50)
#获取entry1的值使用entry1.get(),取得的值是str类型
entry2=tk.Entry(win)
entry2.place(x=190,y=80,width=50)
entry3=tk.Entry(win)
entry3.place(x=250,y=80,width=50)
#scrolledtext滚动文本组件,height表示组件最多能显示7行
scr1 = scrolledtext.ScrolledText(win, width=25, height=7)
scr1.place(x=550,y=80)
#色差值
label3=tk.Label(win,text="色差值",width=10, font=("隶书", 18)).place(x=400,y=200)
scr2 = scrolledtext.ScrolledText(win, width=25, height=7)
scr2.place(x=370,y=230)
#下拉菜单控件
cbox=ttk.Combobox(win)
cbox.place(x=250,y=400)
cbox['value'] = ('请选择色差公式','CIELAB','CMC','CIE94','CIE2000')
cbox.current(0)
# 绑定下拉菜单事件
cbox.bind("<<ComboboxSelected>>")
b = tk.Button(win, text="计算",width=5,height=1,bg="pink",command=count).place(x=450,y=400)
win.mainloop()
这还只是我要写的项目中的一个功能,还有好几个功能需要写,我现在已经快接近300行代码了,所以想把这分割开来写,希望大佬帮忙看下能不能放在两个模块里面
|
|