tkinter编写一个页面东西能放在两个py文件中吗?
用tkinter编写一个界面时,方法内容太多像将它放在两个py文件中,但是不知道怎么弄希望大佬们能给点建议,告诉我怎么弄,谢谢 我看你一直在纠结这个问题,你代码量很大吗?如果不是必须放两个模块何必纠结,如果真的必须放两个模块那也要看你的代码写的适不适合 做不做界面和逻辑的分离,就像楼上所说的,取决于你的代码复杂程度,一般的小程序根本没有必要(除非你准备在后期给你的程序做大量的扩展)
我们做界面和逻辑分离的原因是,当我们修改界面代码时,不用考虑业务逻辑,反过来也是一样。
要做界面和逻辑分离,最重要的点就是,要把更新界面的方法和处理业务的方法分别放在不同的函数中(是否把这些函数放在不同的代码文件中实现,并不是重点),要达到这个目的,那么界面更新的函数和业务处理的函数之间就需要某种通信机制,我不清楚tkinter中是否有比较合适的方法,但是在我经常中的pyqt和pyside中是有这样的机制的,叫做信号槽机制,通过使用信号和槽函数就能实现界面和业务代码的分离。
很抱歉,没有能直接解决你的问题,但是希望能给你一点参考~ 你发那么多帖子问这个,别人回复你,你一条都没回复,行不行会不会也不说,让别人瞎蒙吗? qq1151985918 发表于 2022-3-17 21:19
我看你一直在纠结这个问题,你代码量很大吗?如果不是必须放两个模块何必纠结,如果真的必须放两个模块那也 ...
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 =
fetched_content = list(map(list, zip(*fetched_content)))
Lsp = fetched_content
asp = fetched_content
bsp = fetched_content
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-Lstd
print('明度差是:{:.4f}'.format(DL))
#饱和度差
Csp = (asp**2+bsp**2)**(1/2)
Cstd = (astd**2 + bstd**2)**(1/2)
DCs=Csp-Cstd
print('饱和度差是:{:.4f}'.format(DCs))
#色度差
Da=asp-astd
Db=bsp-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 - Lstd
print('明度差是:{:.4f}'.format(DL))
# 饱和度差
Csp = (asp ** 2 + bsp ** 2) ** (1 / 2)
Cstd = (astd ** 2 + bstd ** 2) ** (1 / 2)
DCs = Csp - Cstd
print('饱和度差是:{:.4f}'.format(DCs))
# 色度差
Da = asp - astd
Db = bsp - 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 - Lstd
print('明度差是:{:.4f}'.format(DL))
# 饱和度差
Csp = (asp ** 2 + bsp ** 2) ** (1 / 2)
Cstd = (astd ** 2 + bstd ** 2) ** (1 / 2)
DCs = Csp - Cstd
print('饱和度差是:{:.4f}'.format(DCs))
# 色度差
Da = asp - astd
Db = bsp - 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**2 + bsp**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
Lstd2=Lstd
bsp2=bsp
bstd2=bstd
asp2=(1+G)*asp
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行代码了,所以想把这分割开来写,希望大佬帮忙看下能不能放在两个模块里面
页:
[1]