繁体   English   中英

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

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

一个简单的消息框案例

我已经想出了如何在简单的 PyQt 对话框窗口中更改字体大小。 举个例子:

    # 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')

更改字体大小的关键是您实际上有一个消息框的“句柄”。 在使用msg.exec_()显示之前,变量msg可用于根据您的需要调整消息框。

一个简单的输入对话框的案例

输入对话框的问题是这样的句柄不存在。 举个例子:

    # 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')

输入对话框对象是即时创建的。 我无法根据需要对其进行调整(例如,应用不同的字体)。

有没有办法在显示之前获取这个QInputDialog对象的句柄?

编辑 :

评论中建议我使用 HTML 片段进行尝试:

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

结果如下:

在此处输入图像描述

如您所见,文本输入字段仍然具有较小(未更改)的字体大小。

感谢@denvaar 和@ekhumoro 的评论,我得到了解决方案。 这里是:

    # 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 由 QLabel、QLineEdit 和 QPushButton 组成。 您可以使用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