首页 文章

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

提问于
浏览
1

The case of a simple message box

我已经想出如何在简单的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')

更改字体大小的关键是您实际上有一个'handle'到您的消息框 . 在使用 msg.exec_() 显示消息框之前,变量 msg 可用于调整消息框 .

The case of a simple input dialog

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

# 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 对象的句柄?

EDIT :

我在评论中建议我使用HTML代码段进行尝试:

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

结果如下:

enter image description here

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

1 回答

  • 2

    感谢@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')
    

相关问题