【发布时间】:2018-04-21 16:39:32
【问题描述】:
我有这个非常简单的 GUI,但我无法让按钮工作。我曾尝试在 clicked.connect 命令中使用 partial 和 lambda,但没有运气。我知道我只能按一次按钮,因为我没有循环来不断更改每个新输入框的位置,但我只想让它先工作。
感谢您的建议
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