import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import *
from pynput import keyboard
class Geo:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def __init__(self, txt, font_size=60, color="white", parent=None):
super(DisplayKeyText, self).__init__(parent)
self.display = txt
self.color = color
self.font_size = font_size
font
= QtGui.
QFont("Helvetica",font_size
) #, QtGui.QFont.Bold) #self.setHtml("<font color='{}' >{}</font>".format(color, self.display))
self.setFont(font)
txt_width = 100
self.setTextWidth(txt_width)
self.document().setTextWidth(txt_width)
self.document().setDefaultTextOption(options)
self.document().setHtml("<font color='{}' >{}</font>".format(color, self.display))
def setTextProps(self, color, font_size, txt=None):
self.setHtml("<font color='{}' size={}>{}</font>"
.format(color, font_size, self.display if txt is None else txt))
def setTextString(self, txt):
self.display = txt
html_txt = "<font color='{}' size={}>{}</font>".format(self.color, self.font_size, txt)
#self.document().setHtml("hello")
self.document().setHtml("<div>hello</div>")
print("x = {}, y = {}, width = {}".format(self.x(), self.y(), self.document().textWidth()))
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.txt = DisplayKeyText("X", 20)
self.scene.addItem(self.txt)
self.create_central_widget()
self.listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release)
self.listener.start()
def create_central_widget(self):
self.
view.
setBackgroundBrush((QColor("bisque"))) self.setCentralWidget(self.view)
self.view.scale(3, 3)
def on_press(self, key):
self.txt.setTextString("*")
def on_release(self, key):
pass
def main():
w = MainWindow()
w.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QPainter, QColor
from pynput import keyboard
class Geo:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
class DisplayKeyText(QtWidgets.QGraphicsTextItem):
def __init__(self, txt, font_size=60, color="white", parent=None):
super(DisplayKeyText, self).__init__(parent)
self.display = txt
self.color = color
self.font_size = font_size
font = QtGui.QFont("Helvetica",font_size) #, QtGui.QFont.Bold)
#self.setHtml("<font color='{}' >{}</font>".format(color, self.display))
self.setFont(font)
txt_width = 100
self.setTextWidth(txt_width)
self.document().setTextWidth(txt_width)
options = QtGui.QTextOption(Qt.AlignHCenter)
self.document().setDefaultTextOption(options)
self.document().setHtml("<font color='{}' >{}</font>".format(color, self.display))
def setTextProps(self, color, font_size, txt=None):
self.setHtml("<font color='{}' size={}>{}</font>"
.format(color, font_size, self.display if txt is None else txt))
def setTextString(self, txt):
self.display = txt
html_txt = "<font color='{}' size={}>{}</font>".format(self.color, self.font_size, txt)
#self.document().setHtml("hello")
self.document().setHtml("<div>hello</div>")
print("x = {}, y = {}, width = {}".format(self.x(), self.y(), self.document().textWidth()))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.scene = QtWidgets.QGraphicsScene(self)
self.scene.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex)
self.txt = DisplayKeyText("X", 20)
self.scene.addItem(self.txt)
self.create_central_widget()
self.listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release)
self.listener.start()
def create_central_widget(self):
self.view = QtWidgets.QGraphicsView(self.scene)
self.view.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)
self.view.setBackgroundBrush((QColor("bisque")))
self.setCentralWidget(self.view)
self.view.scale(3, 3)
def on_press(self, key):
self.txt.setTextString("*")
def on_release(self, key):
pass
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
To copy to clipboard, switch view to plain text mode
Bookmarks