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

时间:2016-09-07 19:14:30

标签: python python-3.x pyqt pyqt5

简单消息框的情况

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

 JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
 String query = object.getString("query");
 JSONArray locations = object.getJSONArray("locations");

更改字体大小的关键是您实际上有一个消息框的“句柄”。在使用 # 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') 对象的句柄?

编辑:

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

QInputDialog

结果如下:

enter image description here

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

1 个答案:

答案 0 :(得分: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')