Python 3.x 我无法让按钮在GUI中工作有任何解决方案吗?

Python 3.x 我无法让按钮在GUI中工作有任何解决方案吗?,python-3.x,pyqt5,Python 3.x,Pyqt5,这是代码。 我正在使用pyCharm和python 3.7 我可以得到带有所有按钮的GUI,但当我点击按钮时,它不会显示在显示框中,直到我点击显示框。也就是说,如果我点击“5”,它不会显示在显示框中,直到我点击显示框。 我用的是MacOsCatalina import sys from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QLabel from PyQt5.QtWidgets import QWidg

这是代码。 我正在使用pyCharm和python 3.7 我可以得到带有所有按钮的GUI,但当我点击按钮时,它不会显示在显示框中,直到我点击显示框。也就是说,如果我点击“5”,它不会显示在显示框中,直到我点击显示框。 我用的是MacOsCatalina



import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from functools import partial

__version__ = "0.1"
ERROR_MSG = "ERROR"

     >The Main GUI class

class calcgui(QMainWindow):
 def __init__(self):
     super().__init__()
     self.setWindowTitle("calc")
     self.setFixedSize(300,300)
     self.generalLayout = QVBoxLayout()
     self._centralWidget = QWidget(self)
     self.setCentralWidget(self._centralWidget)
     self._centralWidget.setLayout(self.generalLayout)
     self._createDisplay()
     self._createButtons()
 def _createDisplay(self):
         self.display = QLineEdit()
         self.display.setFixedHeight(50)
         self.display.setAlignment(Qt.AlignRight)
         self.display.setReadOnly(True)
         self.generalLayout.addWidget(self.display)
 def _createButtons(self):
     self.buttons = {}
     buttonsLayout = QGridLayout()
     buttons = {"7":(0,0),
                 "8":(0,1),
                 "9":(0,2),
                 "C":(0,3),
                 "/":(0,4),
                 "4":(1,0),
                 "5":(1,1),
                 "6":(1,2),
                 "*":(1,3),
                 "(":(1,4),
                 "1":(2,0),
                 "2":(2,1),
                 "3":(2,2),
                 "-":(2,3),
                 ")":(2,4),
                 "0":(3,0),
                 "00":(3,1),
                 ".":(3,2),
                 "+":(3,3),
                 "=":(3,4)
         }
     for btnText, pos in buttons.items():
             self.buttons[btnText] = QPushButton(btnText)
             self.buttons[btnText].setFixedSize(50,50)
             buttonsLayout.addWidget(self.buttons[btnText],pos[0],pos[1])
             self.generalLayout.addLayout(buttonsLayout)

 def setDisplayText(self, text):
     self.display.setText(text)
     self.display.setFocus()

 def DisplayText(self):
     return self.display.text()

 def clearDisplay(self):
     self.setDisplayText("")

    >This is the linking class

class pycalcu:
 def __init__(self,model,view):
     self._evaluate = model
     self._view = view
     self._connectSignals()
 def _calculateResult(self):
     result = self._evaluate(expression=self._view.DisplayText())
     self._view.setDisplayText(result)
 def _buildExpression(self,sub_exp):
     if self._view.DisplayText() == ERROR_MSG:
         self._view.clearDisplay()
     expression = self._view.DisplayText() + sub_exp
     self._view.setDisplayText(expression)
 def _connectSignals(self):
     for btnText, btn in self._view.buttons.items():
         if btnText not in {"C","="}:
             btn.clicked.connect(partial(self._buildExpression,btnText))
     self._view.buttons["="].clicked.connect(self._calculateResult)
     return self._view.display.returnPressed.connect(self._calculateResult)
     self._view.buttons["C"].clicked.connect(self._view.clearDisplay)
def evaluateExpression(expression):
 try:
     result = str(eval(expression, {}, {}))
 except Exception:
     result = ERROR_MSG
 return result
def main():
 pycalc = QApplication(sys.argv)
 view = calcgui()
 view.show()
 model = evaluateExpression
 pycalcu(model=model,view=view)
 sys.exit(pycalc.exec_())
if __name__ == "__main__":
 main()


对不起,我没有深入研究你的程序逻辑。 我注意到了我所做的改变。 试试看

import sys
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget, QMainWindow,
                             QGridLayout, QVBoxLayout, QLineEdit, QPushButton)
from PyQt5.QtCore import Qt
from functools import partial

__version__ = "0.1"
ERROR_MSG = "ERROR"

#     >The Main GUI class

class calcgui(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("calc")
        self.setFixedSize(300,300)

        self._centralWidget = QWidget(self)
        self.setCentralWidget(self._centralWidget)
        self.generalLayout = QVBoxLayout(self._centralWidget)

        self._createDisplay()
        self._createButtons()

    def _createDisplay(self):
        self.display = QLineEdit()
        self.display.setFixedHeight(50)
        self.display.setAlignment(Qt.AlignRight)
        self.display.setReadOnly(True)
        self.generalLayout.addWidget(self.display)

    def _createButtons(self):
        self.buttons = {}
        buttonsLayout = QGridLayout()
        buttons = {
                 "7":(0,0),
                 "8":(0,1),
                 "9":(0,2),
                 "C":(0,3),
                 "/":(0,4),
                 "4":(1,0),
                 "5":(1,1),
                 "6":(1,2),
                 "*":(1,3),
                 "(":(1,4),
                 "1":(2,0),
                 "2":(2,1),
                 "3":(2,2),
                 "-":(2,3),
                 ")":(2,4),
                 "0":(3,0),
                 "00":(3,1),
                 ".":(3,2),
                 "+":(3,3),
                 "=":(3,4)
        }
        for btnText, pos in buttons.items():
            self.buttons[btnText] = QPushButton(btnText)
            self.buttons[btnText].setFixedSize(50,50)
            buttonsLayout.addWidget(self.buttons[btnText],pos[0],pos[1])

        # <----    
        self.generalLayout.addLayout(buttonsLayout)                        # <----

    def setDisplayText(self, text):
        self.display.setText(text)
        self.display.setFocus()

    def DisplayText(self):
        return self.display.text()

    def clearDisplay(self):
        self.setDisplayText("")

#    >This is the linking class

class pycalcu:
    def __init__(self,model, view):
        self._evaluate = model
        self._view = view
        self._connectSignals()

    def _calculateResult(self):
        result = self._evaluate(expression=self._view.DisplayText())
        self._view.setDisplayText(result)

    def _buildExpression(self, sub_exp):
        if self._view.DisplayText() == ERROR_MSG:
            self._view.clearDisplay()
        expression = self._view.DisplayText() + sub_exp
        self._view.setDisplayText(expression)

    def _connectSignals(self):
        for btnText, btn in self._view.buttons.items():
            if btnText not in {"C", "="}:
                btn.clicked.connect(partial(self._buildExpression, btnText))
        self._view.buttons["="].clicked.connect(self._calculateResult)

        self._view.buttons["C"].clicked.connect(self._view.clearDisplay)        # +++

        return self._view.display.returnPressed.connect(self._calculateResult)

# ?        self._view.buttons["C"].clicked.connect(self._view.clearDisplay)     # ---

def evaluateExpression(expression):
    try:
        result = str(eval(expression, {}, {}))
    except Exception:
        result = ERROR_MSG
    return result

def main():
    pycalc = QApplication(sys.argv)
    view = calcgui()
    view.show()

    model = evaluateExpression
    pycalcu(model=model, view=view)

    sys.exit(pycalc.exec_())

if __name__ == "__main__":
    main()
导入系统 从PyQt5.QtWidgets导入(QApplication、QLabel、QWidget、QMainWindow、, QGridLayout、QVBoxLayout、QLineEdit、QPushButton) 从PyQt5.QtCore导入Qt 从functools导入部分 __版本=“0.1” ERROR\u MSG=“ERROR” #>主GUI类 类calcgui(QMainWindow): 定义初始化(自): super()。\uuuu init\uuuuu() self.setWindowTitle(“计算”) 自设置固定大小(300300) self.\u centralWidget=QWidget(self) self.setCentralWidget(self.\u centralWidget) self.generallalyout=QVBoxLayout(self.\u centralWidget) self._createDisplay() self._createButtons() def_createDisplay(自): self.display=QLineEdit() 自显示设置固定高度(50) self.display.setAlignment(Qt.AlignRight) self.display.setReadOnly(True) self.generalLayout.addWidget(self.display) 定义创建按钮(自): self.buttons={} buttonsLayout=QGridLayout() 按钮={ "7":(0,0), "8":(0,1), "9":(0,2), “C”:(0,3), "/":(0,4), "4":(1,0), "5":(1,1), "6":(1,2), "*":(1,3), "(":(1,4), "1":(2,0), "2":(2,1), "3":(2,2), "-":(2,3), ")":(2,4), "0":(3,0), "00":(3,1), ".":(3,2), "+":(3,3), "=":(3,4) } 对于btnText,按钮中的位置。项() self.buttons[btnText]=QPushButton(btnText) self.buttons[btnText].setFixedSize(50,50) buttonsLayout.addWidget(self.buttons[btnText],位置[0],位置[1])
#在代码
self.generalLayout.addLayout(按钮布局)中注意
这不应该在For循环中,因为您只需要在创建完
按钮布局后设置它,这是
>主GUI类
代码的一部分吗?好的,我在上面注释了您的代码
>主GUI类
>这是链接类
,方法是在然后将self.generalLayout.addLayout(buttonsLayout)
移动到For循环之外,代码运行正常,生成您设计的计算器。知道了吗?还是同样的问题,直到单击显示框,显示屏才显示数字或运算符。你认为这可能是因为我的操作系统。而且“C”按钮也不能清除文本框。