簡體   English   中英

彈出包含消息和用戶輸入的消息

[英]Pop up message that include a message and user input

我需要創建一個彈出消息,其中包含一個聲明和一個詢問用戶輸入的問題。 現在我使用此代碼有兩個單獨的彈出窗口:

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

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

我該怎么做才能將它們都包含在同一個彈出窗口中,輸入對話框上方的消息?

試試吧:

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_())

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM