python - PyQt5:有人可以在这段代码中添加一个菜单栏吗?
问题描述
我是 Python 新手,一直在尝试在此代码中添加一个菜单栏以用于文本编辑器。有人可以帮忙并使用文件和编辑添加一个简单的菜单栏吗?这些功能无关紧要,我只想显示一个菜单栏。请帮忙!
#!/usr/bin/python3
# QcodeEditor.py by acbetter.
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtWidgets import QWidget, QPlainTextEdit, QTextEdit
from PyQt5.QtGui import QColor, QPainter, QTextFormat
class QLineNumberArea(QWidget):
def __init__(self, editor):
super().__init__(editor)
self.codeEditor = editor
def sizeHint(self):
return QSize(self.editor.lineNumberAreaWidth(), 0)
def paintEvent(self, event):
self.codeEditor.lineNumberAreaPaintEvent(event)
class QCodeEditor(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.lineNumberArea = QLineNumberArea(self)
self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
self.updateRequest.connect(self.updateLineNumberArea)
self.cursorPositionChanged.connect(self.highlightCurrentLine)
self.updateLineNumberAreaWidth(0)
def lineNumberAreaWidth(self):
digits = 1
max_value = max(1, self.blockCount())
while max_value >= 10:
max_value /= 10
digits += 1
space = 3 + self.fontMetrics().width('9') * digits
return space
def updateLineNumberAreaWidth(self, _):
self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
def updateLineNumberArea(self, rect, dy):
if dy:
self.lineNumberArea.scroll(0, dy)
else:
self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
if rect.contains(self.viewport().rect()):
self.updateLineNumberAreaWidth(0)
def resizeEvent(self, event):
super().resizeEvent(event)
cr = self.contentsRect()
self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height()))
def highlightCurrentLine(self):
extraSelections = []
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
lineColor = QColor(Qt.yellow).lighter(160)
selection.format.setBackground(lineColor)
selection.format.setProperty(QTextFormat.FullWidthSelection, True)
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
extraSelections.append(selection)
self.setExtraSelections(extraSelections)
def lineNumberAreaPaintEvent(self, event):
painter = QPainter(self.lineNumberArea)
painter.fillRect(event.rect(), Qt.lightGray)
block = self.firstVisibleBlock()
blockNumber = block.blockNumber()
top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
bottom = top + self.blockBoundingRect(block).height()
# Just to make sure I use the right font
height = self.fontMetrics().height()
while block.isValid() and (top <= event.rect().bottom()):
if block.isVisible() and (bottom >= event.rect().top()):
number = str(blockNumber + 1)
painter.setPen(Qt.black)
painter.drawText(0, top, self.lineNumberArea.width(), height, Qt.AlignRight, number)
block = block.next()
top = bottom
bottom = top + self.blockBoundingRect(block).height()
blockNumber += 1
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
codeEditor = QCodeEditor()
codeEditor.show()
sys.exit(app.exec_())
这段代码是由某个人发布在 StackOverflow 上的。不幸的是,我不能直接联系他,也不能对他的帖子发表评论,因为我是新来的。因此,非常感谢任何帮助。
解决方案
在大多数主窗口样式的应用程序中,您将使用 QMainWindow 中提供的 menuBar() 函数,将 QMenus 添加到菜单栏并将 QActions 添加到弹出菜单。有关如何创建菜单栏的说明,请参见代码文本。试试看:
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtWidgets import (QWidget, QPlainTextEdit, QTextEdit,
QMainWindow, QAction, qApp)
from PyQt5.QtGui import QColor, QPainter, QTextFormat, QKeySequence
class QLineNumberArea(QWidget):
def __init__(self, editor):
super().__init__(editor)
self.codeEditor = editor
def sizeHint(self):
return QSize(self.editor.lineNumberAreaWidth(), 0)
def paintEvent(self, event):
self.codeEditor.lineNumberAreaPaintEvent(event)
class QCodeEditor(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.lineNumberArea = QLineNumberArea(self)
self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
self.updateRequest.connect(self.updateLineNumberArea)
self.cursorPositionChanged.connect(self.highlightCurrentLine)
self.updateLineNumberAreaWidth(0)
def lineNumberAreaWidth(self):
digits = 1
max_value = max(1, self.blockCount())
while max_value >= 10:
max_value /= 10
digits += 1
space = 3 + self.fontMetrics().width('9') * digits
return space
def updateLineNumberAreaWidth(self, _):
self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
def updateLineNumberArea(self, rect, dy):
if dy:
self.lineNumberArea.scroll(0, dy)
else:
self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
if rect.contains(self.viewport().rect()):
self.updateLineNumberAreaWidth(0)
def resizeEvent(self, event):
super().resizeEvent(event)
cr = self.contentsRect()
self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height()))
def highlightCurrentLine(self):
extraSelections = []
if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
lineColor = QColor(Qt.yellow).lighter(160)
selection.format.setBackground(lineColor)
selection.format.setProperty(QTextFormat.FullWidthSelection, True)
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
extraSelections.append(selection)
self.setExtraSelections(extraSelections)
def lineNumberAreaPaintEvent(self, event):
painter = QPainter(self.lineNumberArea)
painter.fillRect(event.rect(), Qt.lightGray)
block = self.firstVisibleBlock()
blockNumber = block.blockNumber()
top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
bottom = top + self.blockBoundingRect(block).height()
# Just to make sure I use the right font
height = self.fontMetrics().height()
while block.isValid() and (top <= event.rect().bottom()):
if block.isVisible() and (bottom >= event.rect().top()):
number = str(blockNumber + 1)
painter.setPen(Qt.black)
painter.drawText(0, top, self.lineNumberArea.width(), height, Qt.AlignRight, number)
block = block.next()
top = bottom
bottom = top + self.blockBoundingRect(block).height()
blockNumber += 1
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class Writer(QMainWindow):
def __init__(self):
super().__init__()
self.form_widget = QCodeEditor()
self.setCentralWidget(self.form_widget)
self.init_ui()
def init_ui(self):
# Create Menu Bar
bar = self.menuBar()
# Create Root Menus
file = bar.addMenu('&File')
edit = bar.addMenu('Edit')
# Create Actions for menus
save_action = QAction('Save', self)
save_action.setShortcuts(QKeySequence.Save)
new_action = QAction('New', self)
new_action.setShortcut('Ctrl+N')
quit_action = QAction('&Quit', self)
quit_action.setShortcut('Ctrl+Q')
find_action = QAction('Find...', self)
replace_action = QAction('Replace...', self)
# Add actions to Menus
file.addAction(new_action)
file.addAction(save_action)
file.addAction(quit_action)
find_menu = edit.addMenu('Find')
find_menu.addAction(find_action)
find_menu.addAction(replace_action)
# Events
# triggered - This signal is emitted when an action is activated by the user;
# for example, when the user clicks a menu option, toolbar button,
# or presses an action's shortcut key combination, or when trigger() was called.
quit_action.triggered.connect(self.quit_trigger)
file.triggered.connect(self.selected)
self.setWindowTitle("My Menus")
self.resize(600, 400)
self.show()
def quit_trigger(self):
qApp.quit()
def selected(self, q):
print(q.text() + ' selected')
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
#codeEditor = QCodeEditor()
#codeEditor.show()
writer = Writer()
sys.exit(app.exec_())
推荐阅读
- python - URL 密码 (netloc) 中是否允许使用问号 (?)?
- javascript - 不要让在输入中写入大于 max 属性的数字
- react-native - 无法使用 Galio 框架显示 Toast
- security - 当这些工具无法在直播中检测到 ADS 时?
- javascript - Nodejs 推荐干净的文件夹结构
- angular - 如何在 Angular 中隐藏 mat-expansion-panel?
- list - 用递归判断两个列表的长度是否相同
- python - 使用 jupyter notebook 从两个数据帧中找到两行中的共同部分
- java - 如何从 keycloak 自定义身份验证器上的 AuthenticationFlowContext 获取访问令牌
- android - 在使用 npx react-native run-android 命令运行 react native 应用程序时,会出现错误