简体   繁体   English

PyQt:如何更改QInputDialog按钮的文本?

[英]PyQt: How can I change the texts of my QInputDialog buttons?

I want to change the text for the buttons (OK, Cancel) in my QInputDialog box. 我想在QInputDialog框中更改按钮的文本(确定,取消)。 How can I achieve this? 我该如何实现?

I want to use Python's gettext to do the translations, not Qt Linguist. 我想使用Python的gettext进行翻译,而不是Qt Linguist。

# Set maximum file size
def maximumFilesize(self):

    # Get user input
    maxsize, ok = QtGui.QInputDialog.getInt(self, "Maximum file size",
        "Enter maximum file size in bytes:", self.maxsize, 1, 1073741824)

    # If OK was clicked...
    if ok:
        if maxsize <= 0:
            message = "Maximum file size cannot be less than 1."
                QtGui.QMessageBox.critical(self, "Error", message)
                return False

        # Set new maximum file size
        self.maxsize = maxsize

The QInputDialog itself contains methods setOkButtonText and setCancelButtonText , however, the static method gitInt you are using creates a QInputDialog object, which is visible only inside the getInt method and is not accessible to you. QInputDialog本身包含方法setOkButtonTextsetCancelButtonText ,但是,您使用的静态方法gitInt创建一个QInputDialog对象,该对象仅在getInt方法内部可见,您无法访问。

I would suggest to create your own InputIntegerDialog(QtGui.QInputDialog) , on which you'll have to manually add a spinBox and set its properties. 我建议您创建自己的InputIntegerDialog(QtGui.QInputDialog) ,在其上您必须手动添加一个spinBox并设置其属性。 You would then be able to change text on both buttons, because the dialog object is under your control, not created somewhere inside PyQt. 然后,您将能够在两个按钮上更改文本,因为对话框对象在您的控制之下,而不是在PyQt内部创建的。

BTW, if you only wish to translate standard buttons, you may want to see this question . 顺便说一句,如果您只希望翻译标准按钮,则可能需要查看此问题

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

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