Уведомления

Группа в Telegram: @pythonsu

#1 Июль 17, 2019 00:09:42

thenexts
Зарегистрирован: 2019-07-16
Сообщения: 2
Репутация: +  0  -
Профиль   Отправить e-mail  

PyQt5

Ребятки всем привет.

Кто подскажет как в PyQt5 редакторе сделать так что-бы можно было делать слова в тексте разного цвета?

Спасибо за рание

Офлайн

#2 Июль 17, 2019 02:52:26

py.user.next
От:
Зарегистрирован: 2010-04-29
Сообщения: 9716
Репутация: +  842  -
Профиль   Отправить e-mail  

PyQt5

Да в Qt Designer можно раскрасить QLabel через rich text.

Пример рабочий с раскрашенным текстом приложил.



Прикреплённый файлы:
attachment colors-2019-07-17-105055.tar (10,0 KБ)

Офлайн

#3 Июль 17, 2019 10:53:37

Rodegast
От: Пятигорск
Зарегистрирован: 2007-12-28
Сообщения: 2679
Репутация: +  182  -
Профиль   Отправить e-mail  

PyQt5

Как редактор называется?



С дураками и сектантами не спорю, истину не ищу.
Ели кому-то правда не нравится, то заранее извиняюсь.

Офлайн

#4 Июль 17, 2019 12:31:07

vic57
Зарегистрирован: 2015-07-07
Сообщения: 893
Репутация: +  126  -
Профиль   Отправить e-mail  

PyQt5

qtextedit поддерживает HTML -тэги
или QSyntaxHighlighter
https://github.com/baoboa/pyqt5/tree/master/examples/richtext

Офлайн

#5 Июль 17, 2019 19:49:35

thenexts
Зарегистрирован: 2019-07-16
Сообщения: 2
Репутация: +  0  -
Профиль   Отправить e-mail  

PyQt5

нашли

#!/usr/bin/python
#-*- coding: utf-8 -*-

import sys

from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QPalette, QSyntaxHighlighter
from PyQt5.QtWidgets import QMainWindow, QApplication, QPlainTextEdit


def format(color, style=''):

word_color = QColor()
word_color.setNamedColor(color)
word_format = QTextCharFormat()
word_format.setForeground(word_color)
if ‘italic’ in style:
word_format.setFontItalic(True)
elif ‘bold’ in style:
word_format.setFontWeight(QFont.Bold)

return word_format

STYLE = {
‘keywords’: format('#F92672'),
‘booleans’: format('#C1FF00'),
‘operators’: format('#F92672'),
‘braces’: format('#BABAB6'),
‘comments’: format('#9EAF9F'),
‘def_class’: format('#66D9EF'),
‘string’: format('#F2C560'),
‘doc_string’: format('#F2C560'),
‘self’: format('#FF9721', ‘italic’),
‘numbers’: format('#66CB64'),
}


class PythonHighlighter(QSyntaxHighlighter):

keywords = [
‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’,
‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’,
‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’,
‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’,
‘while’, ‘with’, ‘yield’, ‘get’,'post',
]

booleans = [
‘True’, ‘False’, ‘None’,
]

operators = [
‘=’,
‘==’, ‘!=’, ‘<’, ‘<=’, ‘>’, ‘>=’,
‘\+’, ‘-’, ‘\*’, ‘/’, ‘//’, ‘\%’, ‘\*\*’,
‘\+=’, ‘-=’, ‘\*=’, ‘/=’, ‘\%=’,
‘\^’, ‘\|’, ‘\&’, ‘\~’, ‘>>’, ‘<<’,
]

braces = [
‘\{’, ‘\}’, '\', ‘\(’, ‘\)’
]

def __init__(self, document):

QSyntaxHighlighter.__init__(self, document)

self.triple_single = (QRegExp(“\'\'\'”), 1, STYLE)
self.triple_double = (QRegExp('\“\”\"'), 2, STYLE)

# RULES

rules =
# r'' regular expression
# used to indicate set of characters
# \b matches the empty string, but only at the beginning or end of the word
# \w matches any alphanumeric chars and the underscore
# \s matches any whitespace char
# * causes the resulting regexp to match 0 or more repetitions of the preceding regexp. ab* = a, ab, abb, abbb..
# + causes the resulting regexp to match 1 or more repetitions of the preceding regexp. ab+ = ab, abb, abbbb..
# ? causes the resulting regexp to match 0 or 1 repetitions of the preceding regexp. ab? = a, ab
# ^ matches the start of the string, and in multi line mode also matches immediately after each new line
# ?: A non-capturing version of regular parentheses
# QRegExp, int, STYLE

rules += [(r'\b%s\b' % keyword, 0, STYLE) for keyword in PythonHighlighter.keywords]
rules += [(r'\b%s\b' % boolean, 0, STYLE) for boolean in PythonHighlighter.booleans]
rules += [(r'%s' % operator, 0, STYLE) for operator in PythonHighlighter.operators]
rules += [(r'%s' % brace, 0, STYLE) for brace in PythonHighlighter.braces]
# Other rules:
rules += [
# self
(r'\bself\b', 0, STYLE),
# string containing double-quote with escape sequence
(r'"*(\\.*)*"', 0, STYLE),
# string containing single-quote with escape sequence
(r"'*(\\.*)*'", 0, STYLE),
# def/class
(r'\bdef\b\s*(\w+)', 1, STYLE),
(r'\bclass\b\s*(\w+)', 1, STYLE),
# from # until new-line
(r'#*', 0, STYLE),
# numbers
(r'\b?+?\b', 0, STYLE),
(r'\b?+(?:\.+)?(?:?+)?\b', 0, STYLE),
# decorator
(r'@[^\n', 0, STYLE),
]
# Build QRegExp for each pattern
self.rules =

def highlightBlock(self, text):
for expression, nth, format in self.rules:

index = expression.indexIn(text, 0)

while index >= 0:
index = expression.pos(nth)

length = len(expression.cap(nth))

self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)

self.setCurrentBlockState(0)

in_multiline = self.match_multiline(text, *self.triple_single)
if not in_multiline:
in_multiline = self.match_multiline(text, *self.triple_double)

def match_multiline(self, text, delimiter, in_state, style):

if self.previousBlockState() == in_state:
start = 0
add = 0
else:
start = delimiter.indexIn(text)
add = delimiter.matchedLength()

while start >= 0:
end = delimiter.indexIn(text, start + add)
if end >= add:
length = end - start + add + delimiter.matchedLength()
self.setCurrentBlockState(0)
else:
self.setCurrentBlockState(in_state)
length = len(text) - start + add

self.setFormat(start, length, style)
start = delimiter.indexIn(text, start + length)

if self.currentBlockState() == in_state:
return True
else:
return False


class Example(QMainWindow):
def __init__(self):
super().__init__()
self.text_editor = QPlainTextEdit()
self.syntax = PythonHighlighter(self.text_editor.document())

self.init_ui()

def init_ui(self):

background_color = QColor()
background_color.setNamedColor('#282821')

color_palette = self.text_editor.palette()
color_palette.setColor(QPalette.Text, Qt.white)
color_palette.setColor(QPalette.Base, background_color)
self.text_editor.setPalette(color_palette)

default_font = self.text_editor.font()
default_font.setPointSize(9)
self.text_editor.setFont(default_font)

self.setWindowTitle('Example')
self.setCentralWidget(self.text_editor)
self.setGeometry(500, 500, 500, 500)

self.show()

if __name__ == ‘__main__’:
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version