From ef0d28bba5d8cae80951d67f31d9be699bac15e4 Mon Sep 17 00:00:00 2001 From: gavin <2861484755@qq.com> Date: Fri, 31 Mar 2017 20:27:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calc.py | 66 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/calc.py b/calc.py index e15ad9f..38930e5 100644 --- a/calc.py +++ b/calc.py @@ -54,43 +54,47 @@ class Calc(QMainWindow): print "点击按钮前:" ,self.x,self.operator,self.y value = self.sender().text() - # 如果点击的是数字,print该数字 + """ + 点击数字的处理: + 1 点数字时,无运算符且有x,累加x + 2 点数字时,有运算符且有y,累加y + 3 点数字时,无运算符且无x,赋值给x + 4 点数字时,有运算符且无Y,赋值给y + """ if value in "0123456789.": - print "您点击的数字是:",value - try: - if self.operator is None: - # 如果点击的是数字的话,保存到x或追加到x - if self.x is None: - self.x = value - else: - self.x +=value - self.text_result.setText(self.x) - else: - if self.y is None: - self.y = value - else: - self.y +=value - self.text_result.setText(self.y) - except Exception, e: - print e - + if self.operator is not None: # 有运算符 + if self.y is not None: #有y + self.y =self.y + value + else:#无y + self.y = value - - #如果点击的是运算符,print该运算符 + else:#无运算符 + if self.x is not None:#有x + self.x = self.x + value + + else: + self.x = value + + """开始处理运算符 + 1.点运算符时记录运算符 + 2.点运算符时记录运算符并计算结果(x,y,oper都有值) + 3.点运算符时记录运算符,并将x置为0(x无值) + """ elif value in "+-*/": - print "print该运算符是:",value self.operator = value - self.text_result.setText("") + if self.x is None: + self.x = 0 + if self.x and self.y and self.operator: + self.calculate() - # 如果点击的是=,print = - - elif value == '=': - print "您点击的是=。" - self.calculate() - self.text_result.setText(self.result) - + """处理点击等号的操作 + 1.点等号时计算结果 (x,y,oper都有值) + 2.点等号时忽略等号 (x,y,oper不都有值) + """ + elif value == "=": + if self.x and self.y and self.operator: + self.calculate() - # 若果点击的是clear,执行clear()方法 else: print "您点击的是C" self.clear() -- Gitee From 73337f4fe7b099276ad9f462667658d5151cc493 Mon Sep 17 00:00:00 2001 From: gavin <2861484755@qq.com> Date: Fri, 31 Mar 2017 20:40:44 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E5=92=8C=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calc.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/calc.py b/calc.py index 38930e5..f5e080f 100644 --- a/calc.py +++ b/calc.py @@ -41,12 +41,13 @@ class Calc(QMainWindow): def calculate(self): """ - 如果x、y、operater不为空,计算结果。 + 如果x、y、operater不为空,计算结果,计算出结果后,清空y,保持x和结果相等。 """ if self.x and self.y and self.operator: s = "%s%s%s"%(self.x,self.operator,self.y) print s - self.result = str(eval(s)) + self.x = self.result = str(eval(s)) + self.y = None self.text_result.setText(self.result) def click_on_button(self): @@ -54,19 +55,20 @@ class Calc(QMainWindow): print "点击按钮前:" ,self.x,self.operator,self.y value = self.sender().text() - """ - 点击数字的处理: - 1 点数字时,无运算符且有x,累加x - 2 点数字时,有运算符且有y,累加y - 3 点数字时,无运算符且无x,赋值给x - 4 点数字时,有运算符且无Y,赋值给y - """ + + # 点击数字的处理: + # 1 点数字时,无运算符且有x,累加x + # 2 点数字时,有运算符且有y,累加y + # 3 点数字时,无运算符且无x,赋值给x + # 4 点数字时,有运算符且无Y,赋值给y + if value in "0123456789.": if self.operator is not None: # 有运算符 if self.y is not None: #有y self.y =self.y + value else:#无y self.y = value + self.text_result.setText(self.y) else:#无运算符 if self.x is not None:#有x @@ -74,12 +76,14 @@ class Calc(QMainWindow): else: self.x = value + self.text_result.setText(self.x) + - """开始处理运算符 - 1.点运算符时记录运算符 - 2.点运算符时记录运算符并计算结果(x,y,oper都有值) - 3.点运算符时记录运算符,并将x置为0(x无值) - """ + # 开始处理运算符 + # 1.点运算符时记录运算符 + # 2.点运算符时记录运算符并计算结果(x,y,oper都有值) + # 3.点运算符时记录运算符,并将x置为0(x无值) + elif value in "+-*/": self.operator = value if self.x is None: @@ -87,10 +91,10 @@ class Calc(QMainWindow): if self.x and self.y and self.operator: self.calculate() - """处理点击等号的操作 - 1.点等号时计算结果 (x,y,oper都有值) - 2.点等号时忽略等号 (x,y,oper不都有值) - """ + # 处理点击等号的操作 + # 1.点等号时计算结果 (x,y,oper都有值) + # 2.点等号时忽略等号 (x,y,oper不都有值) + elif value == "=": if self.x and self.y and self.operator: self.calculate() -- Gitee From 4210661bd587f2d32266728415617c24f452143d Mon Sep 17 00:00:00 2001 From: gavin <2861484755@qq.com> Date: Fri, 31 Mar 2017 21:04:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=86map=E4=BF=AE=E6=94=B9=E6=88=90for?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=98=93=E8=AF=BB?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/calc.py b/calc.py index f5e080f..ffff18c 100644 --- a/calc.py +++ b/calc.py @@ -37,7 +37,9 @@ class Calc(QMainWindow): self.pushButton_dot, self.pushButton_equal ] - map(lambda btn: btn.clicked.connect(self.click_on_button), btns) + + for btn in btns: + btn.clicked.connect(self.click_on_button) def calculate(self): """ -- Gitee