【问题标题】:Pop up message that include a message and user input包含消息和用户输入的弹出消息
【发布时间】:2019-03-24 11:50:37
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python user-interface pyqt popup


    【解决方案1】:

    试试看:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2011-10-28
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多