简体   繁体   English

在 PyQt5 DialogButtonBox 中使用 3 个按钮

[英]Using 3 Buttons in PyQt5 DialogButtonBox

I'm attempting to create a Login System type dialog box for practice using PyQt5 (I'm quite new to the module) and i'm trying to give the user the ability to click (Ok, Cancel, Apply) as the buttons underneath inputs boxes for Username / Password, but i'm not sure how I can actually get the apply button to work.我正在尝试使用 PyQt5 创建一个登录系统类型对话框以进行练习(我对模块很陌生),并且我试图让用户能够单击(确定、取消、应用)作为下面的按钮用户名/密码的输入框,但我不确定我如何才能真正让应用按钮工作。 I have buttons.accepted.connect(*method*) and buttons.rejected.connect(*method*) but I don't know how to specify the pressing of the accept button.我有buttons.accepted.connect(*method*)buttons.rejected.connect(*method*)但我不知道如何指定按下接受按钮。 I have tried using buttons.clicked(dlgButtons[0] (Which is where the button is stored) but it just gives me an error.我曾尝试使用buttons.clicked(dlgButtons[0] (这是存储按钮的位置),但它只是给我一个错误。

The code below is my declaration of the buttons if that helps.如果有帮助,下面的代码是我对按钮的声明。 Thanks谢谢

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

One possible solution might look like this:一种可能的解决方案可能如下所示:

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

在此处输入图像描述

What you are storing in the dlgButtons is just a list of enums , specifically the StandardButton enum, which is a list of identifiers for the buttons, they are not the "actual" buttons.您在dlgButtons中存储的只是枚举列表,特别是StandardButton枚举,它是按钮标识符的列表,它们不是“实际”按钮。

Also, you cannot use the clicked signal like this:此外,您不能像这样使用clicked信号:

 buttons.clicked(dlgButtons[0])

That will generate a crash, as signals are not callable.这将产生崩溃,因为信号不可调用。 The argument of the clicked() signal is what will be received from the slot, which means that if you connect a function to that signal, the function will receive the clicked button: clicked()信号的参数是将从插槽接收的内容,这意味着如果您将 function 连接到该信号,则 function 将接收点击按钮:

        buttons.clicked.connect(self.buttonsClicked)

    def buttonsClicked(self, button):
        print(button.text())

The above will print the text of the clicked button (Ok, Apply, Cancel, or their equivalent localized text).以上将打印单击按钮的文本(确定、应用、取消或它们等效的本地化文本)。

What you're looking for is to connect to the clicked signals of the actual buttons, and you can get the individual reference to each button by using the button() function:您正在寻找的是连接到实际按钮的点击信号,您可以使用button() function 获得对每个按钮的单独引用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM