第三天:使用wxpython制作计算器

#coding="utf-8"
import wx

# 这是代码:
class get_the_data(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, '丹丹计算器',
                          size=(250, 300))
        panel = wx.Panel(self, -1)

        # 这是一个基本的静态文本
        wx.StaticText(panel, -1, "选择运算符:",
                      (5, 10))
        list=["+","-","*","/"]
        self.operation = wx.ComboBox(panel, -1, value='+', choices=list, style=wx.CB_SORT, pos=(110,5), size=(40,25))

        wx.StaticText(panel, -1, "数字:", (5, 40))
        self.number1=wx.TextCtrl(panel, pos=(110,40), size=(40,20))
        self.number2 = wx.TextCtrl(panel, pos=(160, 40), size=(40, 20))

        wx.StaticText(panel, -1, "求值:", (5, 70))
        self.btn=wx.Button(panel, label="=", pos=(110,70),size=(90,20))
        self.Bind(wx.EVT_BUTTON, self.operation_Digital,self.btn)

        wx.StaticText(panel, -1, "值为:", (5, 110))
        self.get_result = wx.TextCtrl(panel,pos=(55,150),size=(100,90),style=wx.TE_MULTILINE|wx.HSCROLL)

    def operation_Digital(self,event):
        num1=self.number1.GetValue()
        num2=self.number2.GetValue()
        op=self.operation.GetValue()
        if num1.isdigit() and num2.isdigit():
            if op == "+":
                result = int(num1) + int(num2)
            elif op == "-":
                result = int(num1) - int(num2)
            elif op == "*":
                result = int(num1) * int(num2)
            elif op == "/":
                if int(num2) == 0:
                    result="对不起除数为0,请重新输入!!!"
                else:
                    result = int(num1) / int(num2)
            else:
                pass
        else:
            result="对不起您输入错误,请重新输入!!!"


        self.get_result.SetValue(str(result))

if __name__ == '__main__':
    app = wx.App()
    frame = get_the_data()
    frame.Show()
    app.MainLoop()

猜你喜欢

转载自www.cnblogs.com/wanglidan-8642/p/10040918.html
今日推荐