简体   繁体   English

如何在 PyQt 中更改 QInputDialog 的字体大小?

[英]How to change the font size of a QInputDialog in PyQt?

The case of a simple message box一个简单的消息框案例

I have figured out how to change the font size in simple PyQt dialog windows.我已经想出了如何在简单的 PyQt 对话框窗口中更改字体大小。 Take this example:举个例子:

    # Create a custom font
    # ---------------------
    font = QFont()
    font.setFamily("Arial")
    font.setPointSize(10)

    # Show simple message box
    # ------------------------
    msg = QMessageBox()
    msg.setIcon(QMessageBox.Question)
    msg.setText("Are you sure you want to delete this file?")
    msg.setWindowTitle("Sure?")
    msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    msg.setFont(font)
    retval = msg.exec_()
    if retval == QMessageBox.Ok:
        print('OK')
    elif retval == QMessageBox.Cancel:
        print('CANCEL')

The key to changing the font size is that you actually have a 'handle' to your message box.更改字体大小的关键是您实际上有一个消息框的“句柄”。 The variable msg is available to tweak the message box to your needs before showing it with msg.exec_() .在使用msg.exec_()显示之前,变量msg可用于根据您的需要调整消息框。

The case of a simple input dialog一个简单的输入对话框的案例

The problem about the input dialog is that such handle is not present.输入对话框的问题是这样的句柄不存在。 Take this example:举个例子:

    # Show simple input dialog
    # -------------------------
    filename, ok = QInputDialog.getText(None, 'Input Dialog', 'Enter the file name:')
    if(ok):
        print('Name of file = ' + filename)
    else:
        print('Cancelled')

The input dialog object is created on-the-fly.输入对话框对象是即时创建的。 I have no way to tweak it to my needs (eg. apply a different font).我无法根据需要对其进行调整(例如,应用不同的字体)。

Is there a way to get a handle to this QInputDialog object, before showing it?有没有办法在显示之前获取这个QInputDialog对象的句柄?

EDIT :编辑 :

I was adviced in the comments to try it with an HTML snippet:评论中建议我使用 HTML 片段进行尝试:

    filename, ok = QInputDialog.getText(None, 'Input Dialog', '<html style="font-size:12pt;">Enter the file name:</html>')

The result is as follows:结果如下:

在此处输入图像描述

As you can see, the text input field has still the small (unchanged) font size.如您所见,文本输入字段仍然具有较小(未更改)的字体大小。

Thanks to the comments of @denvaar and @ekhumoro, I got the solution.感谢@denvaar 和@ekhumoro 的评论,我得到了解决方案。 Here it is:这里是:

    # Create a custom font
    # ---------------------
    font = QFont()
    font.setFamily("Arial")
    font.setPointSize(10)

    # Create and show the input dialog
    # ---------------------------------
    inputDialog = QInputDialog(None)
    inputDialog.setInputMode(QInputDialog.TextInput)
    inputDialog.setWindowTitle('Input')
    inputDialog.setLabelText('Enter the name for this new file:')
    inputDialog.setFont(font)
    ok = inputDialog.exec_()
    filename = inputDialog.textValue()
    if(ok):
        print('Name of file = ' + filename)
    else:
        print('Cancelled')

QInputDialog consists of QLabel, QLineEdit, and QPushButton. QInputDialog 由 QLabel、QLineEdit 和 QPushButton 组成。 You can independently control their style using setStyleSheet method.您可以使用setStyleSheet方法独立控制它们的样式。

def RequireInputText(Parent, Title="TextInput", Text="Text"):
    InputDialog = QInputDialog(None)
    InputDialog.setInputMode(QInputDialog.TextInput)
    InputDialog.setWindowTitle(Title)
    InputDialog.setLabelText(Text)

    InputDialog.setStyleSheet(
        """
        QLabel{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
        }
        QLineEdit{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
        }
        QPushButton{
            font-size:20px;
            font-weight:bold;
            font-family:Arial;
            border-style:solid;
            border-color:black;
            border-width:2px; 
        }
        """
    )

    Ok = InputDialog.exec_()
    if(Ok):
        InputText = InputDialog.textValue()
        return InputText
    else:
        return None

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

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