弹出包含消息和用户输入的消息

时间:2019-03-24 11:50:37

标签: python user-interface pyqt popup

我需要创建一个弹出消息,其中包含一条语句和一个询问用户输入的问题。现在,我使用此代码有两个单独的弹出窗口:

QtWidgets.QMessageBox.about(self, "Baseline", "Part {}\nThreshold: {}".format(i, threshold)) 

detect_thres, ok = QtWidgets.QInputDialog.getDouble(self,"Input Detection Threshold: ","enter a number")

我应该怎么做才能将它们都包含在输入对话框上方的同一弹出窗口中?

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5.QtWidgets import *

class ModelessDialog(QDialog):
    def __init__(self, part, threshold, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Baseline")
        self.setGeometry(800, 275, 300, 200)
        self.part = part
        self.threshold = threshold
        self.threshNew = 4.4

        label    = QLabel("Part            : {}\nThreshold   : {}".format(
                                                self.part, self.threshold))
        self.label2 = QLabel("ThreshNew : {:,.2f}".format(self.threshNew))

        self.spinBox = QDoubleSpinBox()
        self.spinBox.setMinimum(-2.3)
        self.spinBox.setMaximum(99)
        self.spinBox.setValue(self.threshNew)
        self.spinBox.setSingleStep(0.02)
        self.spinBox.valueChanged.connect(self.valueChang)

        buttonBox = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        layout = QVBoxLayout()            
        layout.addWidget(label)
        layout.addWidget(self.label2)
        layout.addWidget(self.spinBox)
        layout.addWidget(buttonBox)
        self.resize(300, 200)  
        self.setLayout(layout)                                 

        okBtn = buttonBox.button(QDialogButtonBox.Ok) 
        okBtn.clicked.connect(self.apply)

        cancelBtn = buttonBox.button(QDialogButtonBox.Cancel)
        cancelBtn.clicked.connect(self.reject)              

    def apply(self):
        print("""
            Part      : {}
            Threshold : {}
            ThreshNew : {:,.2f}""".format(
                self.part, self.threshold, self.spinBox.value()))

    def valueChang(self):
        self.label2.setText("ThreshNew : {:,.2f}".format(self.spinBox.value()))


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label  = QLabel('Hello Dialog', self)
        button = QPushButton('Open Dialog', self)
        button.clicked.connect(self.showDialog)

        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(button)
        self.setLayout(layout)        

    def showDialog(self):
        self.dialog = ModelessDialog(2, 55.77, self)
        self.dialog.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = Window()
    win.resize(300, 200)
    win.show()
    sys.exit(app.exec_())

enter image description here