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

标签 python python-3.x pyqt pyqt5

简单消息框的案例

我已经弄清楚如何在简单的 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 可用于在使用 msg.exec_() 显示之前根据您的需要调整消息框。

简单输入对话框的案例

关于输入对话框的问题是没有这样的句柄。举个例子:

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

结果如下:

enter image description here

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

最佳答案

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

关于python - 如何在 PyQt 中更改 QInputDialog 的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377515/

相关文章:

python - __new__ = 类型错误 : object() takes no parameters

python - 编写一个与 wpa_supplicant 交互的外部程序

Python 异常 - 如何自动设置 args 属性?

python - 将 matplotlib 图表嵌入到 Qt/C++ 应用程序中

python - 将 bytearray 转换为 array.array ('B' )

python - 如何在 awk 或 sed 中编写查找所有函数(使用正则表达式)

python - 无法使用 python api 发布,登录状态 - KeyError : "name=' loggedin', 域=无,路径=无”

python - 如何将打印件发送到以特定名称保存的 word 文档?

python - subprocess.popen 在 pyqt 中不起作用

python - 暂时抑制 PyQt 事件?