生活随笔
收集整理的這篇文章主要介紹了
基于python3.7的4环电阻读数工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PS:圖片是網上找的
效果圖:
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%')
)
pres
=['','','','','']
class AppFrameC51Resistor4():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_pngtk
.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] = iself
.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] = iself
.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] = iself
.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] = iself
.face1_res_calc
()def face1_res_calc(self
):if pres
[4]!='':self
.tk_face1_res_f
['text']=pcolor
[pres
[4]][5]fvalue
=0funit
=''if pres
[0]=='' or pres
[1]=='' or pres
[3]=='' or pres
[4]=='':returnfvalue
=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
/1000if funit
=='Ω':funit
='K'elif funit
=='K':funit
='M'else:passself
.tk_face1_res_v
['text']=str(round(fvalue
,2))+funit
def destory(self
, ):self
.initface
.destroy
()def setCenter(groot
):winWidth
,winHeight
= 800,600groot
.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
()
總結
以上是生活随笔為你收集整理的基于python3.7的4环电阻读数工具的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。