在pyqt

时间:2018-09-02 13:24:29

标签: python python-3.x pyqt pyqt5

在以下程序中,我试图在pyqt5中创建一个计算器。连接计算器按钮的信号时遇到麻烦。

class Calci(QWidget):
    def __init__(self, Parent=None):

       super(Calci,self).__init__()
       self.initgui()

    def initgui(self):
       list1= ["%","rt","pow","1/x","CE","C","BCK",
              "/","7","8","9","*","4","5","6","-",
              "1","2","3","+","+-","0",".","="]
       list2=[(i,j) for i in range(2,8) for j in range(0,4)]
       self.button={}
       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(list1,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: self.click(x))


    def click(self,n):

         self.lineedit.insert(n)

app=QApplication(sys.argv)
calci=Calci()
calci.show()
app.exec_()

我正在使用以下行self.button [y] .clicked.connect(lambda:self.click(x)) 反映按到Qlineedit上的按钮,但问题是我按的那个按钮仅给出“ =”,这是list1中的最后一项。我认为循环中没有问题,因为所有按钮在被按下时都给出响应,但是它们都给出相同的响应。

我的问题是:所有按钮都已连接,就像每当我按下一个按钮时,它都会给出响应,但没有给出正确的响应。我应该对代码进行哪些更改才能获得正确的代码响应。还有其他方法可以尝试连接信号吗?

0 个答案:

没有答案