gpt4 book ai didi

python - PyQt5,向选项卡添加文本不起作用

转载 作者:行者123 更新时间:2023-12-01 09:17:26 24 4
gpt4 key购买 nike

我正在用 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] 的选项卡,但内容不在其中。

最佳答案

问题很简单,您不必要地创建了许多QPlainTextEdit,例如openFile()方法中有一个QPlainTextEdit,您传递给方法 addtab(),但如果您检查方法 addtab() 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_())

关于python - PyQt5,向选项卡添加文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com