PyQt5,将文本添加到选项卡不起作用
Posted
技术标签:
【中文标题】PyQt5,将文本添加到选项卡不起作用【英文标题】:PyQt5, adding text to tabs doesn't work 【发布时间】:2018-06-30 11:40:11 【问题描述】:我正在用 python 制作一个 IDE,但在我的选项卡中添加文本时遇到了一些问题。
添加标题有效,但在其中添加文本无效。
这是我的代码:
import sys
import sys
from subprocess import PIPE, Popen
import json
from pyautogui import hotkey
from PyQt5 import QtGui, QtPrintSupport
from PyQt5.QtCore import QRect, QRegExp, Qt
from PyQt5.QtGui import (QColor, QFont, QPainter,
QSyntaxHighlighter, QTextCharFormat, QTextCursor)
from PyQt5.QtWidgets import (QAction, QApplication, QDialog, QFileDialog, QPushButton,
QHBoxLayout, QInputDialog, QMainWindow,
QMessageBox, QPlainTextEdit, QVBoxLayout, QWidget, QTabWidget,
qApp, QMenuBar, QStatusBar)
lineBarColor = QColor(53, 53, 53)
lineHighlightColor = QColor('#00FF04')
class NumberBar(QWidget):
def __init__(self, parent=None):
super().__init__()
self.editor = parent
layout = QVBoxLayout()
self.setLayout(layout)
self.editor.blockCountChanged.connect(self.update_width)
self.editor.updateRequest.connect(self.update_on_scroll)
self.update_width('1')
def mousePressEvent(self, QMouseEvent):
print("class NumberBar(QWidget):mousePressEvent")
def update_on_scroll(self, rect, scroll):
if self.isVisible():
if scroll:
self.scroll(0, scroll)
else:
self.update()
def update_width(self, string):
width = self.fontMetrics().width(str(string)) + 10
print("update_width:width:" + str(width))
if self.width() != width:
self.setFixedWidth(width)
def paintEvent(self, event):
if self.isVisible():
block = self.editor.firstVisibleBlock()
height = self.fontMetrics().height()
number = block.blockNumber()
painter = QPainter(self)
painter.fillRect(event.rect(), lineBarColor)
painter.drawRect(0, 0, event.rect().width() - 1, event.rect().height() - 1)
font = painter.font()
current_block = self.editor.textCursor().block().blockNumber() + 1
while block.isValid():
block_geometry = self.editor.blockBoundingGeometry(block)
offset = self.editor.contentOffset()
block_top = block_geometry.translated(offset).top()
number += 1
rect = QRect(0, block_top, self.width() - 5, height)
if number == current_block:
font.setBold(True)
else:
font.setBold(False)
painter.setFont(font)
painter.drawText(rect, Qt.AlignRight, '%i' % number)
if block_top > event.rect().bottom():
break
block = block.next()
painter.end()
class Content(QWidget):
def __init__(self, text):
super(Content, self).__init__()
self.editor = QPlainTextEdit()
# Create a layout for the line numbers
self.hbox = QHBoxLayout()
self.setLayout(self.hbox)
self.numbers = NumberBar(self.editor)
self.hbox.addWidget(self.numbers)
self.hbox.addWidget(self.editor)
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.editor = QPlainTextEdit()
# Initialize tab screen
self.tabs = QTabWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.closeTab)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def addtab(self, content, fileName):
self.tabs.addTab(Content(str(content)), str(fileName))
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.open()
self.tabs = MyTableWidget(self)
#self.tabs.addtab("nga", "sa")
self.setCentralWidget(self.tabs)
self.initUI()
self.show()
def initUI(self):
self.statusBar()
menu = self.menuBar()
fileMenu = menu.addMenu('File')
fileMenu.addAction(self.openAct)
self.resize(800, 600)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def buttonClicked(self):
self.tabs.addTab(Content("smalltext2"), "sadsad")
def open(self):
self.openAct = QAction('Open...', self)
self.openAct.setShortcut('Ctrl+O')
self.openAct.setStatusTip('Open a file')
self.is_opened = False
self.openAct.triggered.connect(self.openFile)
def openFile(self):
options = QFileDialog.Options()
self.files, _ = QFileDialog.getOpenFileNames(
self, 'Open a file', '',
'All Files (*);;Python Files (*.py);;Text Files (*.txt)',
options=options
)
self.files = self.files[0]
if self.files:
with open(self.files, 'r+') as file_o:
print(self.files)
text = file_o.read()
text_widget = QPlainTextEdit(self.tabs)
text_widget.setPlainText(text)
self.tabs.addtab(text_widget, self.files[0])
print(text)
if __name__ == '__main__':
with open("../config.json", "r") as jsonFile:
read = jsonFile.read()
data = json.loads(read)
app = QApplication(sys.argv)
app.setStyle('Fusion')
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Window, QColor(data["editor"][0]["windowColor"]))
palette.setColor(QtGui.QPalette.WindowText, QColor(data["editor"][0]["windowText"]))
palette.setColor(QtGui.QPalette.Base, QColor(data["editor"][0]["editorColor"]))
palette.setColor(QtGui.QPalette.AlternateBase, QColor(data["editor"][0]["alternateBase"]))
palette.setColor(QtGui.QPalette.ToolTipBase, QColor(data["editor"][0]["ToolTipBase"]))
palette.setColor(QtGui.QPalette.ToolTipText, QColor(data["editor"][0]["ToolTipText"]))
palette.setColor(QtGui.QPalette.Text, QColor(data["editor"][0]["editorText"]))
palette.setColor(QtGui.QPalette.Button, QColor(data["editor"][0]["buttonColor"]))
palette.setColor(QtGui.QPalette.ButtonText, QColor(data["editor"][0]["buttonTextColor"]))
palette.setColor(QtGui.QPalette.Highlight, QColor(data["editor"][0]["HighlightColor"]).lighter())
palette.setColor(QtGui.QPalette.HighlightedText, QColor(data["editor"][0]["HighlightedTextColor"]))
app.setPalette(palette)
ex = Main()
sys.exit(app.exec_())
在第 171 行,我尝试添加一个选项卡,其中包含我打开的文件 self.tabs.addtab(text_widget, self.files[0])
的内容。
它确实创建了名称为 self.files[0]
的选项卡,但内容不在其中。
【问题讨论】:
【参考方案1】:问题其实很简单,你创建了很多不必要的QPlainTextEdit
s,例如openFile()
方法中有一个QPlainTextEdit
你传递给方法addtab()
,但是如果你检查方法@987654325 @MyTableWidget
需要一个字符串而不是 QPlainTextEdit
,MyTableWidget
中还有另一个 QPlainTextEdit
不必要。
去掉不必要的元素我们得到如下:
import sys
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, \
QVBoxLayout, QTabWidget, QFileDialog, QPlainTextEdit, QHBoxLayout
lineBarColor = QColor(53, 53, 53)
lineHighlightColor = QColor('#00FF04')
class NumberBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.editor = parent
layout = QVBoxLayout(self)
self.editor.blockCountChanged.connect(self.update_width)
self.editor.updateRequest.connect(self.update_on_scroll)
self.update_width('1')
def mousePressEvent(self, QMouseEvent):
print("class NumberBar(QWidget):mousePressEvent")
def update_on_scroll(self, rect, scroll):
if self.isVisible():
if scroll:
self.scroll(0, scroll)
else:
self.update()
def update_width(self, string):
width = self.fontMetrics().width(str(string)) + 10
print("update_width:width:" + str(width))
if self.width() != width:
self.setFixedWidth(width)
def paintEvent(self, event):
if self.isVisible():
block = self.editor.firstVisibleBlock()
height = self.fontMetrics().height()
number = block.blockNumber()
painter = QPainter(self)
painter.fillRect(event.rect(), lineBarColor)
painter.drawRect(0, 0, event.rect().width() - 1, event.rect().height() - 1)
font = painter.font()
current_block = self.editor.textCursor().block().blockNumber() + 1
while block.isValid():
block_geometry = self.editor.blockBoundingGeometry(block)
offset = self.editor.contentOffset()
block_top = block_geometry.translated(offset).top()
number += 1
rect = QRect(0, block_top, self.width() - 5, height)
if number == current_block:
font.setBold(True)
else:
font.setBold(False)
painter.setFont(font)
painter.drawText(rect, Qt.AlignRight, '%i' % number)
if block_top > event.rect().bottom():
break
block = block.next()
painter.end()
class Content(QWidget):
def __init__(self, text):
super(Content, self).__init__()
self.editor = QPlainTextEdit()
self.editor.setPlainText(text)
# Create a layout for the line numbers
self.hbox = QHBoxLayout(self)
self.numbers = NumberBar(self.editor)
self.hbox.addWidget(self.numbers)
self.hbox.addWidget(self.editor)
class MyTableWidget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
# Initialize tab screen
self.tabs = QTabWidget()
self.tabs.resize(300, 200)
# Add tabs
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.closeTab)
# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def addtab(self, content, fileName):
self.tabs.addTab(Content(str(content)), str(fileName))
class Main(QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.open()
self.tabs = MyTableWidget()
#self.tabs.addtab("nga", "sa")
self.setCentralWidget(self.tabs)
self.initUI()
self.show()
def initUI(self):
self.statusBar()
menu = self.menuBar()
fileMenu = menu.addMenu('File')
fileMenu.addAction(self.openAct)
self.resize(800, 600)
def closeTab(self, index):
tab = self.tabs.widget(index)
tab.deleteLater()
self.tabs.removeTab(index)
def buttonClicked(self):
self.tabs.addTab(Content("smalltext2"), "sadsad")
def open(self):
self.openAct = QAction('Open...', self)
self.openAct.setShortcut('Ctrl+O')
self.openAct.setStatusTip('Open a file')
self.is_opened = False
self.openAct.triggered.connect(self.openFile)
def openFile(self):
options = QFileDialog.Options()
filenames, _ = QFileDialog.getOpenFileNames(
self, 'Open a file', '',
'All Files (*);;Python Files (*.py);;Text Files (*.txt)',
options=options
)
if filenames:
for filename in filenames:
with open(filename, 'r+') as file_o:
text = file_o.read()
self.tabs.addtab(text, filename)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())
【讨论】:
以上是关于PyQt5,将文本添加到选项卡不起作用的主要内容,如果未能解决你的问题,请参考以下文章
在 UITabcontroller 中转到 3 选项卡不起作用
使用UI-Bootstrap 0.11.0,角度js中的活动选项卡不起作用