【问题标题】:Using 3 Buttons in PyQt5 DialogButtonBox在 PyQt5 DialogBu​​ttonBox 中使用 3 个按钮
【发布时间】:2020-12-16 10:38:33
【问题描述】:

我正在尝试创建一个登录系统类型对话框以练习使用 PyQt5(我对该模块很陌生),并且我试图让用户能够单击(确定、取消、应用)作为用户名/密码输入框下方的按钮,但我不确定我如何才能真正让应用按钮工作。我有buttons.accepted.connect(*method*)buttons.rejected.connect(*method*) 但我不知道如何指定按下接受按钮。我曾尝试使用buttons.clicked(dlgButtons[0](这是存储按钮的位置),但它只会给我一个错误。

如果有帮助,下面的代码是我对按钮的声明。谢谢

buttons = qt.QDialogButtonBox()
        dlgButtons = (qt.QDialogButtonBox.Apply, qt.QDialogButtonBox.Ok, qt.QDialogButtonBox.Cancel)
        buttons.setStandardButtons(
        dlgButtons[0] | dlgButtons[1] | dlgButtons[2]
        )

【问题讨论】:

    标签: python pyqt5


    【解决方案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
                | QDialogButtonBox.Apply)
    
            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._okBtn)
    
            cancelBtn = buttonBox.button(QDialogButtonBox.Cancel)
            cancelBtn.clicked.connect(self.reject)   
    
            applyBtn = buttonBox.button(QDialogButtonBox.Apply)       # +++
            applyBtn.clicked.connect(self._apply)                     # +++
    
        def _apply(self):                                             # +++
            print('Hello Apply')    
    
        def _okBtn(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_())
    

    【讨论】:

    • 但问题是我不知道如何为应用按钮分配功能,而且我对这个模块还很陌生,无法真正说出你在哪里完成的。编辑:没关系,我只是个白痴,没有看到我可以向下滚动,忽略我,谢谢你的回答
    • @Nyaywraith 我为你标记了行# +++
    【解决方案2】:

    您在dlgButtons 中存储的只是枚举 的列表,特别是StandardButton 枚举,它是按钮的标识符 列表,它们不是“实际”按钮。

    另外,您不能像这样使用clicked 信号:

     buttons.clicked(dlgButtons[0])
    

    这将导致崩溃,因为信号不可调用。 clicked() 信号的参数是将从插槽接收的内容,这意味着如果您将函数连接到该信号,该函数将接收单击的按钮:

            buttons.clicked.connect(self.buttonsClicked)
    
        def buttonsClicked(self, button):
            print(button.text())
    

    上面将打印单击按钮的文本(确定、应用、取消或它们等效的本地化文本)。

    您正在寻找的是连接到实际按钮的点击信号,您可以使用button() 函数获取对每个按钮的单独引用:

    applyButton = buttons.button(qt.QDialogButtonBox.Apply)
    applyButton.clicked.connect(self.applyFunction)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      相关资源
      最近更新 更多