PyQt计算器-没有得到结果

时间:2018-09-14 05:34:44

标签: python-3.x pyqt5

在以下程序中,当我按“ =”获取结果时,计算器gui正在关闭。我认为eval函数存在一些问题。如果我删除评估,那么程序没有任何问题。需要帮助弄清楚这一点。除此以外,还有其他方法可以尝试吗?预先感谢。

    class Calci(QWidget):

        def __init__(self, Parent=None):



            super(Calci,self).__init__()

            self.initgui()



        def initgui(self):

            self.list1= 

   ["%","rt","pow","1/x","CE","C","BCK","/","7","8","9","*","4","5","6","- 
   ","1","2","3","+","+-","0",".","="]

            self.list2=[(i,j) for i in range(2,8) for j in range(0,4)]

            self.button={}

            self.data1=""

            self.data2=0

            self.lineedit=QLineEdit()

            self.lineedit.setFocus()

            grid=QGridLayout() 

            self.setLayout(grid)

            self.font=QFont("Bookman Old Style",15,25)

            self.lineedit.setFont(self.font)

            for x,y in zip(self.list1,self.list2):



                self.button[y]=QPushButton(x)

                grid.addWidget(self.lineedit,0,0,2,4)


    self.lineedit.setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Expanding)

                self.lineedit.setMinimumHeight(70)

                grid.addWidget(self.button[y],*y)





   self.button[y].setSizePolicy(QSizePolicy.Preferred,QSizePolicy.Expanding)

                self.button[y].setMinimumHeight(70)

                self.lineedit.setAlignment(Qt.AlignRight)

                self.button[y].clicked.connect(lambda state,x=x: self.click(x))





        def click(self,n):





                if (n=="="):



                   data1=self.lineedit.text()



                   self.lineedit.clear()

                   self.lineedit.insert(eval(data1))



                else:

                   self.lineedit.insert(n)













    app=QApplication(sys.argv)

    calci=Calci()

    calci.show()

    app.exec_()

0 个答案:

没有答案