1. 程式人生 > >第三天:使用wxpython製作計算器

第三天:使用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()