wxPython色环电阻计算器

 更新时间:2019年11月18日 15:37:49   作者:吴越南蛮  
这篇文章主要为大家详细介绍了wxPython色环电阻计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了wxPython色环电阻计算器的具体代码,供大家参考,具体内容如下

import wx # 导入wxPython
class MyFrame(wx.Frame):
 def __init__(self,parent,id):
  wx.Frame.__init__(self, parent, id, "色环电阻计算器2.0",size=(600,450))
  self.panel = wx.Panel(self) # 创建画板(设置程序标题,大小)
  self.font1 = wx.Font(20,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
  self.font2 = wx.Font(16,wx.DEFAULT,wx.FONTSTYLE_NORMAL,wx.NORMAL,faceName="黑体")
  self.choices = ['黑', '棕', '红', '橙', '黄', '绿', '蓝', '紫', '灰', '白', '金', '银']
  self.choices2 = ['黑', '棕', '红', '绿', '蓝', '紫', '灰', '金', '银']
  self.choices3 = ['black','brown','red','coral','yellow','green','blue','purple','grey','white','gold','light grey']
  self.choices4 = ['black','brown','red','green','blue','purple','grey','gold','light grey']
  self.error_code = ['20', '1', '2', '0.5', '0.25', '0.1', '0.05', '5', '10']
  self.IsFive = True
  self.Init_Panel()

 def Init_Panel(self):
  self.Create_display_part()
  self.Create_resistant()
  self.bt_change = wx.Button(self.panel, label='切换为4色环', pos=(400, 250), size=(150, 50))
  self.bt_change.SetFont(self.font2)
  self.bt_change.Bind(wx.EVT_BUTTON, self.Event_Change)
  self.Create_display5()

 def Event_Change(self,event):
  self.radiobox1.Destroy()
  self.radiobox2.Destroy()
  self.radiobox3.Destroy()
  self.radiobox4.Destroy()
  self.colour_1.Destroy()
  self.colour_2.Destroy()
  self.colour_3.Destroy()
  self.colour_4.Destroy()
  if self.IsFive == True:
   self.bt_change.SetLabel('切换为5色环')
   self.radiobox5.Destroy()
   self.colour_5.Destroy()
   self.Create_display4()
   self.IsFive = False
  else:
   self.bt_change.SetLabel('切换为4色环')
   self.Create_display5()
   self.IsFive = True

 def Event_radiobox(self,event):
  self.colour_1.Destroy()
  self.colour_2.Destroy()
  self.colour_3.Destroy()
  self.colour_4.Destroy()
  one = self.radiobox1.GetSelection()
  two = self.radiobox2.GetSelection()
  three = self.radiobox3.GetSelection()
  four = self.radiobox4.GetSelection()
  if self.IsFive == True:
   self.colour_5.Destroy()
   five = self.radiobox5.GetSelection()
   if four > 9:
    temp = 9 - four
   else:
    temp = four
   result = (one * 100 + two * 10 + three) * (pow(10, temp))
   error = self.error_code[five]
   self.display(result, error)
   self.colour_1 = self.Create_Colourbar(200, self.choices3[one])
   self.colour_2 = self.Create_Colourbar(240, self.choices3[two])
   self.colour_3 = self.Create_Colourbar(280, self.choices3[three])
   self.colour_4 = self.Create_Colourbar(320, self.choices3[four])
   self.colour_5 = self.Create_Colourbar(360, self.choices4[five])
  else:
   if three > 8:
    temp = 8 - three
   else:
    temp = three + 1
   result = (one * 10 + two) * (pow(10, temp))
   error = self.error_code[four]
   self.display(result, error)
   self.colour_1 = self.Create_Colourbar(200, self.choices3[one])
   self.colour_2 = self.Create_Colourbar(250, self.choices3[two])
   self.colour_3 = self.Create_Colourbar(300, self.choices3[three + 1])
   self.colour_4 = self.Create_Colourbar(350, self.choices4[four])

 def Create_display_part(self):
  label_resistance = wx.StaticText(self.panel, label="阻值:",pos=(400,25))
  label_error = wx.StaticText(self.panel, label="误差:±",pos=(400,125))
  label_percentage = wx.StaticText(self.panel, label="%",pos=(520,150))
  self.label_ohm = wx.StaticText(self.panel, label="Ω", pos=(520, 50))
  self.text_resistance = wx.TextCtrl(self.panel, value='0.00',pos=(400, 50), style=wx.TE_RIGHT | wx.TE_READONLY)
  self.text_error = wx.TextCtrl(self.panel, value='20',pos=(400, 150), style=wx.TE_RIGHT | wx.TE_READONLY)
  label_resistance.SetFont(self.font2)
  label_error.SetFont(self.font2)
  label_percentage.SetFont(self.font2)
  self.label_ohm.SetFont(self.font2)
  self.text_resistance.SetFont(self.font2)
  self.text_error.SetFont(self.font2)
  self.text_resistance.SetBackgroundColour('white')
  self.text_error.SetBackgroundColour('white')

 def Create_resistant(self):
  body = wx.StaticText(self.panel, pos=(170, 330), size=(240, 50))
  body.SetBackgroundColour('light blue')
  left_pin = wx.StaticText(self.panel, pos=(20, 350), size=(150, 5))
  left_pin.SetBackgroundColour('white')
  right_pin = wx.StaticText(self.panel, pos=(410, 350), size=(150, 5))
  right_pin.SetBackgroundColour('white')

 def Create_display4(self):
  self.radiobox1 = self.Create_radiobox('1',0,self.choices[:-2])
  self.radiobox2 = self.Create_radiobox('2',100, self.choices[:-2])
  self.radiobox3 = self.Create_radiobox('3',200, self.choices[1:])
  self.radiobox4 = self.Create_radiobox('4',300, self.choices2)
  self.colour_1 = self.Create_Colourbar(200,'black')
  self.colour_2 = self.Create_Colourbar(250,'black')
  self.colour_3 = self.Create_Colourbar(300,'brown')
  self.colour_4 = self.Create_Colourbar(350,'black')

 def Create_display5(self):
  self.radiobox1 = self.Create_radiobox('1', 0, self.choices[:-2])
  self.radiobox2 = self.Create_radiobox('2', 75, self.choices[:-2])
  self.radiobox3 = self.Create_radiobox('3', 150, self.choices[:-2])
  self.radiobox4 = self.Create_radiobox('4', 225, self.choices)
  self.radiobox5 = self.Create_radiobox('5', 300, self.choices2)
  self.colour_1 = self.Create_Colourbar(200, 'black')
  self.colour_2 = self.Create_Colourbar(240, 'black')
  self.colour_3 = self.Create_Colourbar(280, 'black')
  self.colour_4 = self.Create_Colourbar(320, 'black')
  self.colour_5 = self.Create_Colourbar(360, 'black')

 def Create_radiobox(self, label_num, abscissa, choices):
  label = '第' + label_num + '位'
  pos = (abscissa,0)
  radiobox = wx.RadioBox(self.panel, -1, label, pos, choices=choices, majorDimension=1)
  radiobox.Bind(wx.EVT_RADIOBOX, self.Event_radiobox)
  return radiobox

 def Create_Colourbar(self,abscissa,colour):
  pos = (abscissa,330)
  colour_bar = wx.StaticText(self.panel,pos=pos, size=(30, 50))
  colour_bar.SetBackgroundColour(colour)
  return colour_bar

 def display(self,a,b):
  if a>=1000000:
   a="%.2f"%(a/1000000)
   self.label_ohm.SetLabel('MΩ')
  elif 1000<=a<=1000000:
   a="%.2f"%(a/1000)
   self.label_ohm.SetLabel('KΩ')
  else:
   a="%.2f"%(a)
   self.label_ohm.SetLabel('Ω')
  self.text_resistance.SetValue(a)
  self.text_error.SetValue(b)

if __name__ =='__main__':
 app = wx.App()
 myframe = MyFrame(None,-1)
 myframe.Show()
 app.MainLoop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python编程中内置的NotImplemented类型的用法

    Python编程中内置的NotImplemented类型的用法

    这篇文章主要介绍了Python编程中内置的NotImplemented类型的用法,NotImplemented 是Python在内置命名空间中的六个常数之一,下文更多详细内容需要的小伙伴可以参考一下
    2022-03-03
  • 基于Python编写一个自动关机程序

    基于Python编写一个自动关机程序

    这篇文章主要介绍了基于Python编写的一个自动关机程序,文中的示例代码讲解详细,对我们学习Python有一定的帮助,感兴趣的同学可以学习一下
    2022-01-01
  • python中字符串的常见操作总结(一)

    python中字符串的常见操作总结(一)

    这篇文章主要介绍了python中字符串的常见操作总结,文章通过简单介绍对象的相关展开全文详细内容,需要的朋友可以参考一下
    2022-07-07
  • Python pandas 重命名索引和列名称的实现

    Python pandas 重命名索引和列名称的实现

    本文主要介绍了Python pandas 重命名索引和列名称的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 详解flask入门模板引擎

    详解flask入门模板引擎

    这篇文章主要介绍了详解flask入门模板引擎,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Python打包exe时各种异常处理方案总结

    Python打包exe时各种异常处理方案总结

    今天教大家用Python打包exe时各种异常处理的方案总结,下文中有非常详细的介绍,对正在学习python的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • 浅谈Python 敏感词过滤的实现

    浅谈Python 敏感词过滤的实现

    这篇文章主要介绍了浅谈Python 敏感词过滤的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python中选择排序的实现与优化

    Python中选择排序的实现与优化

    选择排序(Selection Sort)是一种简单但有效的排序算法,本文将详细介绍选择排序算法的原理和实现,并提供相关的Python代码示例,需要的可以参考一下
    2023-06-06
  • 解决jupyter notebook打不开无反应 浏览器未启动的问题

    解决jupyter notebook打不开无反应 浏览器未启动的问题

    这篇文章主要介绍了解决jupyter notebook打不开无反应 浏览器未启动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • 在python中写个自定义数据包协议的打包和解包测试

    在python中写个自定义数据包协议的打包和解包测试

    这篇文章主要介绍了在python中写个自定义数据包协议的打包和解包测试,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09

最新评论