Python 如何在PyQt中更改对话框的字体大小?
简单消息框的情况 我已经了解了如何在简单的PyQt对话框窗口中更改字体大小。举个例子:Python 如何在PyQt中更改对话框的字体大小?,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,简单消息框的情况 我已经了解了如何在简单的PyQt对话框窗口中更改字体大小。举个例子: # Create a custom font # --------------------- font = QFont() font.setFamily("Arial") font.setPointSize(10) # Show simple message box # ------------------------ msg = QMessa
# 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')
# 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')
更改字体大小的关键在于,您实际上拥有消息框的“句柄”。变量msg
可根据您的需要调整消息框,然后再使用msg.exec\ux()
显示消息框
简单输入对话框的情况
输入对话框的问题是这样的句柄不存在。举个例子:
# 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')
# 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>')
filename,ok=QInputDialog.getText(无,“输入对话框”,“输入文件名:”)
结果如下:
如您所见,文本输入字段的字体仍然很小(未更改)。多亏了@denvaar和@ekhumaro的评论,我得到了解决方案。这是:
# 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')
您可以使用HTML作为文本,并设置
style=“font-size:10px;”“
这是个好主意!我会试试看。尽管如此,我还是会保留这个问题,因为其他调整(与字体大小无关)可能需要QInputDialog
object.Hi@denvaar的句柄。您的方法有效,但仅适用于显示的信息文本。输入文本字段仍然是默认字体。你能不能不做一些像input\u dialog=QInputDialog
然后input\u dialog.setFont(font)
?我不知道应该给QInputDialog
的构造函数什么参数。你知道,有几种类型的输入对话框。。