[英]Fonts Menu PyQt5 Text Editor
問題:
我正在嘗試找到一種為我的PyQt5文本編輯器程序的用戶編寫的文本添加字體樣式的方法。 我不想手動將每種字體編碼到某種菜單中,並且我想知道是否存在一種內置方式供用戶選擇文本的字體樣式,如下所示(記事本字體選擇器):
我的代碼當前如下所示:
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Text Editor'
self.left = 10
self.top = 10
self.width = 1080
self.height = 920
self.widget = QWidget(self)
self.lbl = QLabel(self)
self.text = QTextEdit(self.widget)
self.widget.setLayout(QVBoxLayout())
self.widget.layout().addWidget(self.text)
self.setCentralWidget(self.widget)
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
toolBar = self.menuBar()
fileMenu = toolBar.addMenu('File')
editMenu = toolBar.addMenu('Edit')
toolsMenu = toolBar.addMenu('Tools')
helpMenu = toolBar.addMenu('Help')
fontButton = QAction('Configure Editor', self)
fontButton.setShortcut('Ctrl+E')
fontButton.triggered.connect(lambda: self.font_set)
toolsMenu.addAction(fontButton)
self.show()
def font_set(self):
print("Display Fonts")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Qt有一個名為QFontDialog
的小部件,非常適合這種情況,在下面的部分中,我將演示其用法示例:
def font_set(self):
font, ok = QFontDialog.getFont(self.text.font(), self)
if ok:
#QApplication.setFont(font)
self.text.setFont(font)
print("Display Fonts", font)
注意:您必須更改以下語句:
fontButton.triggered.connect(lambda: self.font_set)
至:
fontButton.triggered.connect(self.font_set)
截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.