1,如何用python编写计算器python内置计算器 ,直接在控制台或cmd窗口输式子回车就会给出答案
2,求用Python写一个简单的计算器python计算器实现:wx.textctrl(文本输入控件),这个作为计算器的显示屏,点击数字时用appendtext方法将数字添加至文本末尾,要取出数字时用getvalue方法,点击运算符时要将文本框中的数字清空(先存储)用clear方法然后还有将操作符保存,点击等号后要将第二个操作数存储,然后用int方法将字符串转换为数字,计算后用setvalue方法将结果送入文本框,具体代码如下:import wx# begin wxglade: extracode# end wxgladeclass myframe(wx.frame): def __init__(self, *args, **kwds): # begin wxglade: myframe.__init__ kwds["style"] = wx.default_frame_style wx.frame.__init__(self, *args, **kwds) self.text_ctrl_1 = wx.textctrl(self, -1, "",style=wx.te_readonly) self.button_37 = wx.button(self, -1, "1") self.button_38 = wx.button(self, -1, "2") self.button_39 = wx.button(self, -1, "3") self.button_40 = wx.button(self, -1, "+") self.button_41 = wx.button(self, -1, "4") self.button_42 = wx.button(self, -1, "5") self.button_43 = wx.button(self, -1, "6") self.button_44 = wx.button(self, -1, "-") self.button_46 = wx.button(self, -1, "7") self.button_45 = wx.button(self, -1, "8") self.button_47 = wx.button(self, -1, "9") self.button_48 = wx.button(self, -1, "x") self.button_49 = wx.button(self, -1, "c") self.button_50 = wx.button(self, -1, "0") self.button_51 = wx.button(self, -1, "=") self.button_52 = wx.button(self, -1, "/") self.__set_properties() self.__do_layout() self.bind(wx.evt_button, self.bu1, self.button_37) self.bind(wx.evt_button, self.bu2, self.button_38) self.bind(wx.evt_button, self.bu3, self.button_39) self.bind(wx.evt_button, self.bu_plus, self.button_40) self.bind(wx.evt_button, self.bu4, self.button_41) self.bind(wx.evt_button, self.bu5, self.button_42) self.bind(wx.evt_button, self.bu6, self.button_43) self.bind(wx.evt_button, self.bu_min, self.button_44) self.bind(wx.evt_button, self.bu7, self.button_46) self.bind(wx.evt_button, self.bu8, self.button_45) self.bind(wx.evt_button, self.bu9, self.button_47) self.bind(wx.evt_button, self.bu_mul, self.button_48) self.bind(wx.evt_button, self.bu_clear, self.button_49) self.bind(wx.evt_button, self.bu0, self.button_50) self.bind(wx.evt_button, self.bu_result, self.button_51) self.bind(wx.evt_button, self.bu_chu, self.button_52) # end wxglade self.show(true) def __set_properties(self): # begin wxglade: myframe.__set_properties self.settitle("python calculater by cyg") self.text_ctrl_1.setminsize((400, 50)) self.button_37.setminsize((100, 50)) self.button_38.setminsize((100, 50)) self.button_39.setminsize((100, 50)) self.button_40.setminsize((100, 50)) self.button_41.setminsize((100, 50)) self.button_42.setminsize((100, 50)) self.button_43.setminsize((100, 50)) self.button_44.setminsize((100, 50)) self.button_46.setminsize((100, 50)) self.button_45.setminsize((100, 50)) self.button_47.setminsize((100, 50)) self.button_48.setminsize((100, 50)) self.button_49.setminsize((100, 50)) self.button_50.setminsize((100, 50)) self.button_51.setminsize((100, 50)) self.button_52.setminsize((100, 50)) # end wxglade def __do_layout(self): # begin wxglade: myframe.__do_layout sizer_2 = wx.boxsizer(wx.vertical) sizer_3 = wx.boxsizer(wx.vertical) grid_sizer_1 = wx.gridsizer(4, 4, 0, 0) sizer_3.add(self.text_ctrl_1, 0, 0, 0) grid_sizer_1.add(self.button_37, 0, 0, 0) grid_sizer_1.add(self.button_38, 0, 0, 0) grid_sizer_1.add(self.button_39, 0, 0, 0) grid_sizer_1.add(self.button_40, 0, 0, 0) grid_sizer_1.add(self.button_41, 0, 0, 0) grid_sizer_1.add(self.button_42, 0, 0, 0) grid_sizer_1.add(self.button_43, 0, 0, 0) grid_sizer_1.add(self.button_44, 0, 0, 0) grid_sizer_1.add(self.button_46, 0, 0, 0) grid_sizer_1.add(self.button_45, 0, 0, 0) grid_sizer_1.add(self.button_47, 0, 0, 0) grid_sizer_1.add(self.button_48, 0, 0, 0) grid_sizer_1.add(self.button_49, 0, 0, 0) grid_sizer_1.add(self.button_50, 0, 0, 0) grid_sizer_1.add(self.button_51, 0, 0, 0) grid_sizer_1.add(self.button_52, 0, 0, 0) sizer_3.add(grid_sizer_1, 1, wx.expand, 0) sizer_2.add(sizer_3, 1, wx.expand, 0) self.setsizer(sizer_2) sizer_2.fit(self) self.layout() # end wxglade def bu1(self, event): # wxglade: myframe.self.text_ctrl_1.appendtext("1") event.skip() def bu2(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("2") event.skip() def bu3(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("3") event.skip() def bu_plus(self, event): # wxglade: myframe. self.num1=self.text_ctrl_1.getvalue() self.op="+" self.text_ctrl_1.clear() event.skip() def bu4(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("4") event.skip() def bu5(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("5") event.skip() def bu6(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("6") event.skip() def bu_min(self, event): # wxglade: myframe. self.num1=self.text_ctrl_1.getvalue() self.op="-" self.text_ctrl_1.clear() event.skip() def bu7(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("7") event.skip() def bu8(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("8") event.skip() def bu9(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("9") event.skip() def bu_mul(self, event): # wxglade: myframe. self.num1=self.text_ctrl_1.getvalue() self.op="x" self.text_ctrl_1.clear() event.skip() def bu_clear(self, event): # wxglade: myframe. self.text_ctrl_1.clear() event.skip() def bu0(self, event): # wxglade: myframe. self.text_ctrl_1.appendtext("0") event.skip() def bu_chu(self, event): # wxglade: myframe. self.num1=self.text_ctrl_1.getvalue() self.op="/" self.text_ctrl_1.clear() event.skip() def bu_result(self, event): # wxglade: myframe. self.num2=self.text_ctrl_1.getvalue() num1=int(self.num1) num2=int(self.num2) if self.op=="+": self.text_ctrl_1.setvalue(str(num1+num2)) elif self.op=="-": self.text_ctrl_1.setvalue(str(num1-num2)) elif self.op=="x": self.text_ctrl_1.setvalue(str(num1*num2)) elif self.op=="/": self.text_ctrl_1.setvalue(str(num1/num2)) event.skip() # end of class myframe if __name__ == "__main__": app=wx.app(false) myframe=myframe(none) app.mainloop()【Python编写计算器,如何用python编写计算器】
推荐阅读
- 杞菊地黄丸的功效与作用
- 学py做什么职业最爽,DNF哪个职业PY最厉害
- 中药地胆头的功效与作用
- 雾北科技脚本下载,北京思维实创科技有限公司 是否真实 向外批量发售手机充值卡 是否属
- 电脑程序编程入门视频,电脑编程入门教学视频
- java开发常用四大框架,java 开发有哪些框架
- 熊胆粉的功效与作用 熊胆粉的药用价值
- 免费领王者皮肤软件真实可靠,有没有可以直接领取王者荣耀皮肤的软件
- iphone主题怎么设置好看,苹果手机主题怎么设置