所有PyQt5小部件都处于同一点

时间:2018-02-20 15:51:28

标签: python python-3.x pyqt pyqt5

我正在将我的python GUI计算器从tkinter重新设置为PyQt5。当我运行程序时,它会显示一个窗口,其中所有按钮位于同一位置。

这是我的第一个PyQt5代码之一,所以任何帮助都会受到赞赏。

我正在使用:

- Python 3.6.4
- PyQt5_gpl-5.10
- 64-bit windows

Here是我的代码的链接。

代码:

import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QPushButton, QLineEdit, QApplication, QGridLayout)


class Calculator(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
#        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('PyQt5 Calculator')
        grid = QGridLayout()

        self.E = QLineEdit(self)
        grid.addWidget(self.E, 0, 0, 0, 6)

#BUTTONS 23
        #0
        self.B0 = QPushButton('0', self)
        grid.addWidget(self.B0, 4, 0)
        self.B0.clicked.connect(lambda:self.action(0))
        #1
        self.B1 = QPushButton('1', self)
        grid.addWidget(self.B1, 3, 0)
        self.B1.clicked.connect(lambda:self.action(1))
        #2
        self.B2 = QPushButton('2', self)
        grid.addWidget(self.B2, 3, 1)
        self.B2.clicked.connect(lambda:self.action(2))
        #3
        self.B3 = QPushButton('3', self)
        grid.addWidget(self.B3, 3, 2)
        self.B3.clicked.connect(lambda:self.action(3))
        #4
        self.B4 = QPushButton('4', self)
        grid.addWidget(self.B4, 2, 0)
        self.B4.clicked.connect(lambda:self.action(4))
        #5
        self.B5 = QPushButton('5', self)
        grid.addWidget(self.B5, 2, 1)
        self.B5.clicked.connect(lambda:self.action(5))
        #6
        self.B6 = QPushButton('6', self)
        grid.addWidget(self.B6, 2, 2)
        self.B6.clicked.connect(lambda:self.action(6))
        #7
        self.B7 = QPushButton('7', self)
        grid.addWidget(self.B7, 1, 0)
        self.B7.clicked.connect(lambda:self.action(7))
        #8
        self.B8 = QPushButton('8', self)
        grid.addWidget(self.B8, 1, 1)
        self.B8.clicked.connect(lambda:self.action(8))
        #9
        self.B9 = QPushButton('9', self)
        grid.addWidget(self.B9, 1, 2)
        self.B9.clicked.connect(lambda:self.action(9))

        #C
        self.Bc = QPushButton('C', self)
        grid.addWidget(self.Bc, 1, 5)
        self.Bc.clicked.connect(lambda:self.clear1())
        #AC
        self.Bac = QPushButton('AC', self)
        grid.addWidget(self.Bac, 1, 4)
        self.Bac.clicked.connect(lambda:self.clear_all())
        #
        self.Bplus = QPushButton('+', self)
        grid.addWidget(self.Bplus, 4, 3)
        self.Bplus.clicked.connect(lambda:self.action("+"))
        #
        self.Bminus = QPushButton('-', self)
        grid.addWidget(self.Bminus, 3, 3)
        self.Bminus.clicked.connect(lambda:self.action("-"))
        #
        self.Btimes = QPushButton('x', self)
        grid.addWidget(self.Btimes, 2, 3)
        self.Btimes.clicked.connect(lambda:self.action("x"))
        #
        self.Bdivide = QPushButton('÷', self)
        grid.addWidget(self.Bdivide, 1, 3)
        self.Bdivide.clicked.connect(lambda:self.action("÷"))
        #
        self.Bpoint = QPushButton('.', self)
        grid.addWidget(self.Bpoint, 4, 1)
        self.Bpoint.clicked.connect(lambda:self.action("."))
        #
        self.Bequals = QPushButton('=', self)
        grid.addWidget(self.Bequals, 4, 4)
        self.Bequals.clicked.connect(lambda:self.action("="))
        #
        self.Blbracket = QPushButton('(', self)
        grid.addWidget(self.Blbracket, 2, 4)
        self.Blbracket.clicked.connect(lambda:self.action("("))
        #
        self.Brbracket = QPushButton(')', self)
        grid.addWidget(self.Brbracket, 2, 5)
        self.Brbracket.clicked.connect(lambda:self.action(")"))
        #
        self.Bsqrt = QPushButton('√', self)
        grid.addWidget(self.Bsqrt, 3, 4)
        self.Bsqrt.clicked.connect(lambda:self.action("√"))
        #
        self.Bsqr = QPushButton('x²', self)
        grid.addWidget(self.Bsqr, 3, 5)
        self.Bsqr.clicked.connect(lambda:self.action("x²"))
        #
        self.Bprocent = QPushButton('%', self)
        grid.addWidget(self.Bprocent, 4, 2)
        self.Bprocent.clicked.connect(lambda:self.action("%"))

        self.show()

    def action(self, button_text):
        self.E.setText(button_text)

    def clear_all(self):
        self.E.clear()

    def clear1(self):
        self.text = self.E.text()[:-1]
        self.E.clear()
        self.E.setText(self.text)


    def getandreplace(self):
        self.replaced_text = self.E.text().replace("÷",'/')
        self.replaced_text = self.replaced_text.replace('x','*')
        return self.replaced_text

    def equals(self):
        self.getandreplace()
        try:
            self.value = eval(self.replaced_text)
        except SyntaxError or NameError or ZeroDivisionError:
            self.E.clear()
            self.E.setText("Invalid input!")
        else:
            self.E.clear()
            self.E.setText(self.value)

    def square(self):
        self.getandreplace()
        try:
            self.value = eval(self.replaced_text)
        except SyntaxError or NameError:
            self.E.clear()
            self.E.setText("Invalid input!")
        else:
            self.sq_val = math.pow(self.value,2)
            self.E.clear()
            self.E.setText(self.sq_val)

    def squareroot(self):
        self.getandreplace()
        try:
            self.value = eval(self.replaced_text)
        except SyntaxError or NameError:
            self.E.clear()
            self.E.setText("Invalid input!")
        else:
            sqrt_val = math.sqrt(value)
            self.E.clear()
            self.E.setText(sqrt_val)   











if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Calculator()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

问题是由于您创建了QGridLayout但未将其分配给窗口小部件引起的,另一个错误是您必须在插入QLineEdit时将rowSpan设置为1:

def initUI(self):
    self.setWindowTitle('PyQt5 Calculator')
    grid = QGridLayout(self)
    self.E = QLineEdit(self)
    grid.addWidget(self.E, 0, 0, 1, 6)

enter image description here

您的代码中的另一个错误是您在点击时传递了一个数字,您必须传递一个字符串然后使用setText()

self.B0.clicked.connect(lambda:self.action('0'))
self.B1.clicked.connect(lambda:self.action('1'))
self.B2.clicked.connect(lambda:self.action('2'))
self.B3.clicked.connect(lambda:self.action('3'))
self.B4.clicked.connect(lambda:self.action('4'))
self.B5.clicked.connect(lambda:self.action('5'))
self.B6.clicked.connect(lambda:self.action('6'))
self.B7.clicked.connect(lambda:self.action('7'))
self.B8.clicked.connect(lambda:self.action('8'))
self.B9.clicked.connect(lambda:self.action('9'))