PS:图片是网上找的
效果图:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import tkinter as tk
from tkinter import ttk
from PIL import Image,ImageDraw,ImageTk
# 名称 段值 乘数 误差
pcolor=(
('黑','black' ,0, 1, 'Ω', ''),
('棕','brown' ,1, 10, 'Ω', '±1%'),
('红','red' ,2, 100,'Ω', '±2%'),
('橙','orange' ,3, 1, 'K',''),
('黄','yellow' ,4, 10, 'K',''),
('绿','green' ,5, 100,'K','±0.5%'),
('蓝','blue' ,6, 1, 'M','±0.25%'),
('紫','violet' ,7, 10, 'M','±0.1%'),
('灰','grey' ,8, '', 'Ω', '±0.05%'),
('白','white' ,9, '', 'Ω', ''),
('金','gold' ,'',0.1,'Ω', '±5%'),
('银','silver' ,'',0.01,'Ω','±10%')
)
#1段、2段、3段、乘数、误差
pres=['','','','','']
# 画面 工具-编程工具
class AppFrameC51Resistor4():
#master为Tk()对象
def __init__(self, master):
self.initface = tk.Frame(master, )
self.initface.grid(row=0, column=0)
#----------------------------- 电阻计算
self.view_1()
#-----------------------------
#-----------------------------
#-----------------------------
#-----------------------------
#色环电阻的识别
def view_1(self):
face1 = tk.Frame(self.initface)
face1.grid(row=0, column=0)
tk.Label(face1, text='色环电阻').grid(row=0, column=0, sticky=tk.W)
im = Image.open('color_resistor.jpg')
im = im.resize((370,412), Image.ANTIALIAS)
img_png = ImageTk.PhotoImage(im)
imglabel = tk.Label(face1)
imglabel.grid(row=1,column=0, rowspan=30)
imglabel.configure(image=img_png)
imglabel.image = img_png
tk.Label(face1, text='第一段').grid(row=2, column=1)
self.tk_face1_f1 = tk.Label(face1, text='无')
self.tk_face1_f1.grid(row=2, column=2)
for i in range(10):
tk.Button(face1, text=pcolor[i][0], bg=pcolor[i][1], command=lambda i=i: self.face1_btn1_click(i)).grid(row=2, column=i+3)
tk.Label(face1, text='第二段').grid(row=3, column=1)
self.tk_face1_f2 = tk.Label(face1, text='无')
self.tk_face1_f2.grid(row=3, column=2)
for i in range(10):
tk.Button(face1, text=pcolor[i][0], bg=pcolor[i][1], command=lambda i=i: self.face1_btn2_click(i)).grid(row=3, column=i+3)
tk.Label(face1, text='乘数').grid(row=5, column=1)
self.tk_face1_fc = tk.Label(face1, text='无')
self.tk_face1_fc.grid(row=5, column=2)
for i in range(12):
if i not in(8,9):
tk.Button(face1, text=pcolor[i][0], bg=pcolor[i][1], command=lambda i=i: self.face1_btnc_click(i)).grid(row=5, column=i+3)
tk.Label(face1, text='误差').grid(row=6, column=1)
self.tk_face1_fw = tk.Label(face1, text='无')
self.tk_face1_fw.grid(row=6, column=2)
for i in range(12):
if i not in(0,3,4,9):
tk.Button(face1, text=pcolor[i][0], bg=pcolor[i][1], command=lambda i=i: self.face1_btnw_click(i)).grid(row=6, column=i+3)
tk.Label(face1, text='电阻').grid(row=7, column=1)
self.tk_face1_res_v = tk.Label(face1, text='')
self.tk_face1_res_v.grid(row=7, column=2, columnspan=3)
tk.Label(face1, text='误差').grid(row=8, column=1)
self.tk_face1_res_f = tk.Label(face1, text='')
self.tk_face1_res_f.grid(row=8, column=2, columnspan=3)
def face1_btn1_click(self,i):
self.tk_face1_f1['bg']=pcolor[i][1]
self.tk_face1_f1['text']=pcolor[i][0]
pres[0] = i
self.face1_res_calc()
def face1_btn2_click(self,i):
self.tk_face1_f2['bg']=pcolor[i][1]
self.tk_face1_f2['text']=pcolor[i][0]
pres[1] = i
self.face1_res_calc()
def face1_btnc_click(self,i):
self.tk_face1_fc['bg']=pcolor[i][1]
self.tk_face1_fc['text']=pcolor[i][0]
pres[3] = i
self.face1_res_calc()
def face1_btnw_click(self,i):
self.tk_face1_fw['bg']=pcolor[i][1]
self.tk_face1_fw['text']=pcolor[i][0]
pres[4] = i
self.face1_res_calc()
def face1_res_calc(self):
#误差
if pres[4]!='':
self.tk_face1_res_f['text']=pcolor[pres[4]][5]
fvalue=0
funit=''
if pres[0]=='' or pres[1]=='' or pres[3]=='' or pres[4]=='':return
fvalue=pcolor[pres[0]][2]*10+pcolor[pres[1]][2]
fvalue=fvalue * pcolor[pres[3]][3]
funit=pcolor[pres[3]][4]
if fvalue>1000 and funit!='M':
fvalue=fvalue/1000
if funit=='Ω':
funit='K'
elif funit=='K':
funit='M'
else:
pass
self.tk_face1_res_v['text']=str(round(fvalue,2))+funit
def destory(self, ):
self.initface.destroy()
def setCenter(groot):
winWidth,winHeight = 800,600
groot.title("我的工具")
screenWidth = groot.winfo_screenwidth()
screenHeight = groot.winfo_screenheight()
x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 设置窗口初始位置在屏幕居中
groot.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 设置窗口宽高固定
groot.resizable(0, 0)
if __name__ == "__main__":
groot = tk.Tk() #窗口
setCenter(groot)
AppFrameC51Resistor4(groot)
groot.mainloop()