【问题标题】:pyqt5 QPushButton() ( with clicked.connect() ) does not workpyqt5 QPushButton() (带有 clicked.connect() )不起作用
【发布时间】:2018-04-21 16:39:32
【问题描述】:

我有这个非常简单的 GUI,但我无法让按钮工作。我曾尝试在 clicked.connect 命令中使用 partiallambda,但没有运气。我知道我只能按一次按钮,因为我没有循环来不断更改每个新输入框的位置,但我只想让它先工作。

感谢您的建议

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QSizePolicy,
                         QDoubleSpinBox, QLabel, QCheckBox, QMainWindow,
QGridLayout)
from PyQt5.QtCore import QCoreApplication
import matplotlib
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar



class MainWindow(QWidget):

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

        self.initUI()

    def initUI(self):

        # Geometry of main window:
        self.setGeometry(200, 200, 1000, 1000)
        self.setWindowTitle('Simulation')

        #---------------------------------------
                # Button for adding blades
        blade_button = QPushButton('Add', self)
        blade_button.clicked.connect(self.add_Bladebox)
        blade_button.move(800, 600)


        #---------------------------------------

        self.show()

    # Method for input box:
    def inputBox(self, left, top, maxvalue, step, default,decimals):
        box = QDoubleSpinBox(self)
        box.move(left,top)
        box.setDecimals(decimals)
        box.setMaximum(maxvalue)
        box.setSingleStep(step)
        box.setProperty("value", default)
        box.resize(box.sizeHint())
        return box

    # Method for adding blade boxes
    def add_Bladebox(self):
        left = 900
        top = 500
        maxvalue = 3
        step=1
        default=0
        decimals=1
        blade_box = self.inputBox(left, top, maxvalue, step, default, decimals)


if __name__ == '__main__':

    app = QCoreApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

【问题讨论】:

  • 什么是 PlotCanvas'?我的代码出现此错误:NameError: name 'PlotCanvas' is not defined
  • 画布后面有一个撇号。这可能是它不起作用的原因。此外,我将删除有效的命令版本并保留无效的命令版本(为清楚起见)。
  • 你也可以完全省略画布。

标签: python qt pyqt pyqt5 qpushbutton


【解决方案1】:

试试这个:

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QSizePolicy,
                         QDoubleSpinBox, QLabel, QCheckBox, QMainWindow,
QGridLayout)
from PyQt5.QtCore import QCoreApplication
import matplotlib
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar



class MainWindow(QWidget):

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

        self.initUI()

    def initUI(self):

        # Geometry of main window:
        self.setGeometry(200, 200, 1000, 1000)
        self.setWindowTitle('Simulation')

        #---------------------------------------
                # Button for adding blades
        blade_button = QPushButton('Add', self)
        blade_button.clicked.connect(self.add_Bladebox)
        blade_button.move(800, 600)


        #---------------------------------------

        self.show()

    # Method for input box:
    def inputBox(self, left, top, maxvalue, step, default,decimals):
        box = QDoubleSpinBox(self)
        box.move(left,top)
        box.setDecimals(decimals)
        box.setMaximum(maxvalue)
        box.setSingleStep(step)
        box.setProperty("value", default)
        box.resize(box.sizeHint())
        box.show()

    # Method for adding blade boxes
    def add_Bladebox(self):

        print('This is Ok')

        left = 900
        top = 500
        maxvalue = 3
        step = 1
        default = 0
        decimals = 1
        self.inputBox(left, top, maxvalue, step, default, decimals)


if __name__ == '__main__':

    app = QCoreApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2014-11-17
    • 2022-07-07
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    相关资源
    最近更新 更多