ich bin Einsteiger und habe mit PyQt5 meine erste grafische Oberfläche erstellt. Ich habe das Problem, dass sich mein Fenster schließt, sobald ich die Enter-Taste drücke (der QPushButton wird ausgelöst).
Das Verhalten ist von mir nicht gewollt und ziemlich nervig. Wenn ich z.B. in der QSpinBox einen Wert eingebe und diesen mit Enter bestätige schließt sich mein Fenster. Mein Code sieht folgendermaßen aus:
Code: Alles auswählen
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class CannyGUI(QtWidgets.QDialog):
def __init__(self):
super(CannyGUI, self).__init__()
self.canny_kernel_current_value = 3
self.canny_low_current_value = 50
self.canny_high_current_value = 150
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(448, 470)
self.layoutWidget = QtWidgets.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 401, 421))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.horizontalSlider_CannyLow = QtWidgets.QSlider(self.layoutWidget)
self.horizontalSlider_CannyLow.setMaximum(255)
self.horizontalSlider_CannyLow.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider_CannyLow.setObjectName(
"horizontalSlider_CannyLow")
self.gridLayout.addWidget(self.horizontalSlider_CannyLow, 3, 0, 1, 2)
self.label_Kernel = QtWidgets.QLabel(self.layoutWidget)
self.label_Kernel.setObjectName("label_Kernel")
self.gridLayout.addWidget(self.label_Kernel, 0, 0, 1, 1)
self.label_Canny_lower_value = QtWidgets.QLabel(self.layoutWidget)
self.label_Canny_lower_value.setObjectName("label_Canny_lower_value")
self.gridLayout.addWidget(
self.label_Canny_lower_value, 2, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.pushButtonClose = QtWidgets.QPushButton(self.layoutWidget)
self.pushButtonClose.setObjectName("pushButtonClose")
self.gridLayout.addWidget(self.pushButtonClose, 6, 0, 1, 2)
self.label_Canny_upper_value = QtWidgets.QLabel(self.layoutWidget)
self.label_Canny_upper_value.setObjectName("label_Canny_upper_value")
self.gridLayout.addWidget(
self.label_Canny_upper_value, 4, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.label_CannyHigh = QtWidgets.QLabel(self.layoutWidget)
self.label_CannyHigh.setObjectName("label_CannyHigh")
self.gridLayout.addWidget(self.label_CannyHigh, 2, 0, 1, 1)
self.horizontalSlider_CannyHigh = QtWidgets.QSlider(self.layoutWidget)
self.horizontalSlider_CannyHigh.setMaximum(255)
self.horizontalSlider_CannyHigh.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider_CannyHigh.setObjectName(
"horizontalSlider_CannyHigh")
self.gridLayout.addWidget(self.horizontalSlider_CannyHigh, 5, 0, 1, 2)
self.label_CannyLow = QtWidgets.QLabel(self.layoutWidget)
self.label_CannyLow.setObjectName("label_CannyLow")
self.gridLayout.addWidget(self.label_CannyLow, 4, 0, 1, 1)
self.spinBoxKernel = QtWidgets.QSpinBox(self.layoutWidget)
self.spinBoxKernel.setObjectName("spinBoxKernel")
self.gridLayout.addWidget(
self.spinBoxKernel, 0, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# connecting the sliders and close-button
self.spinBoxKernel.valueChanged.connect(self.spin_kernel)
self.horizontalSlider_CannyLow.valueChanged.connect(
self.slide_canny_low)
self.horizontalSlider_CannyHigh.valueChanged.connect(
self.slide_canny_high)
self.pushButtonClose.clicked.connect(self.close_app)
# set slider properties
self.spinBoxKernel.setRange(3, 15)
self.spinBoxKernel.setValue(3)
self.spinBoxKernel.setSingleStep(2)
self.horizontalSlider_CannyLow.setValue(5.5)
self.horizontalSlider_CannyHigh.setValue(150)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label_Kernel.setToolTip(_translate(
"Dialog", "<html><head/><body><p>sets Kernelsize for Canny-edgedetector</p></body></html>"))
self.label_Kernel.setText(_translate(
"Dialog", "<html><head/><body><p align=\"center\">Kernel size</p></body></html>"))
self.label_Canny_lower_value.setText(_translate(
"Dialog", "<html><head/><body><p align=\"center\">0</p></body></html>"))
self.pushButtonClose.setText(_translate("Dialog", "close"))
self.label_Canny_upper_value.setText(_translate(
"Dialog", "<html><head/><body><p align=\"center\">255</p></body></html>"))
self.label_CannyHigh.setToolTip(_translate(
"Dialog", "<html><head/><body><p>high boundary</p></body></html>"))
self.label_CannyHigh.setText(_translate(
"Dialog", "<html><head/><body><p align=\"center\">lower threshold</p></body></html>"))
self.label_CannyLow.setToolTip(_translate(
"Dialog", "<html><head/><body><p>low boundary</p></body></html>"))
self.label_CannyLow.setText(_translate(
"Dialog", "<html><head/><body><p align=\"center\">upper threshold</p></body></html>"))
# define behaviour of widgets
def spin_kernel(self, value):
print("New Kernelsize: %s" % (value))
self.canny_kernel_current_value = (value)
return self.canny_kernel_current_value
def slide_canny_low(self, value):
self.label_Canny_lower_value.setText(str(value))
print("New lower boundary: %s" % value)
self.canny_low_current_value = value
return self.canny_low_current_value
def slide_canny_high(self, value):
self.label_Canny_upper_value.setText(str(value))
print("New upper boundary: %s" % value)
self.canny_high_current_value = value
return self.canny_high_current_value
def close_app(self):
print("Canny-App has been closed")
QtCore.QCoreApplication.instance().quit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = CannyGUI()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Würde mich über Tipps freuen.