I am seeing gap into QVBoxLayout when I am placing layout it does not look good, any idea to fix issue
#!/usr/bin/env python
import os
import sys
from PySide2.QtCore import Qt, QSize, QRegExp, QFile, QTextStream
from PySide2.QtGui import QKeySequence, QTextCharFormat, QBrush,QColor, QTextDocument, QTextCursor
from PySide2.QtWidgets import (QApplication, QDesktopWidget, QMainWindow,
QPlainTextEdit, QGridLayout, QGroupBox,
QFormLayout, QHBoxLayout, QLabel, QLineEdit,
QMenu, QMenuBar, QPushButton, QMessageBox,
QTextEdit, QVBoxLayout, QWidget, QAction)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.width = 1100
self.height = 700
self.set_main_window()
self.create_violation_text()
def set_main_window(self):
"""
Setting main window position
"""
self.setWindowTitle("GUI %s" %(os.path.abspath(__file__)))
self.setFixedSize(QSize(self.width, self.height))
wid = QDesktopWidget()
screen_width = wid.screen().frameGeometry().width()
screen_height = wid.screen().frameGeometry().height()
self.setGeometry(screen_width/2-self.width/2,
screen_height/2-self.height/2,
self.width, self.height)
def create_violation_text(self):
"""
creating main violation window which contain all violations
"""
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setLineWrapMode(QPlainTextEdit.NoWrap)
self.plain_textedit.setStyleSheet(
"""QPlainTextEdit {font-size: 14pt;
font-family: Courier;}""")
class ReviewWindow(MainWindow):
def __init__(self, waiver_files, app):
"""creating Review mode"""
super().__init__()
self.waiver_files = waiver_files
self.app = app
def create_widget(self):
self.create_text_finder()
self.create_violation_window()
#order matter
main_layout = QVBoxLayout()
main_layout.addWidget(self.text_finder)
main_layout.addWidget(self.create_violation_box)
#creating widgeth object as main window does not
#add layout directly it add only widget
window = QWidget()
window.setLayout(main_layout)
self.setCentralWidget(window)
def create_text_finder(self):
"""
create text finder which wil search string into document
"""
self.text_finder = QGroupBox()
layout = QHBoxLayout()
label = QLabel()
label.setText("Keyword:")
self.line_edit = QLineEdit()
self.line_edit.setText("Enter your search here")
push_button = QPushButton("Find")
push_button.clicked.connect(self.find_string_match)
layout.addWidget(label)
layout.addWidget(self.line_edit)
layout.addWidget(push_button)
self.text_finder.setLayout(layout)
def create_violation_window(self):
"""
creating violation window which contain text editor,
violation type and checkbox
"""
self.create_violation_box = QGroupBox()
layout = QGridLayout()
layout.addWidget(self.plain_textedit, 1, 0, 12, 1)
layout.setColumnMinimumWidth(0, 10)
self.create_violation_box.setLayout(layout)
def find_string_match(self):
"""
Adding string match operation
"""
#format for desire match
format = QTextCharFormat()
format.setBackground(QBrush(QColor("red")))
pattern = QRegExp(self.line_edit.text())
text_document = self.plain_textedit.document()
#Reverting if any
text_document.undo()
if pattern.isEmpty():
QMessageBox.warning("Search filed is empty")
else:
find_cursor = QTextCursor(text_document)
cursor = QTextCursor(text_document)
cursor.beginEditBlock()
while (not find_cursor.isNull() and not find_cursor.atEnd()):
found = False
find_cursor = text_document.find(pattern, find_cursor,
QTextDocument.FindWholeWords)
if (not find_cursor.isNull()):
found = True
find_cursor.movePosition(QTextCursor.WordRight,
QTextCursor.KeepAnchor)
find_cursor.mergeCharFormat(format)
cursor.endEditBlock()
if __name__ == '__main__':
app = QApplication(sys.argv)
cwd = os.path.dirname(__file__)
waiver_file = \[os.path.join(cwd, fdata) for fdata in os.listdir(cwd)\]
window = ReviewWindow(waiver_file, app)
window.create_widget()
window.show()
app.exec_()
Related
The window shows a set of pictures. Once the window is maximized the app shows all pics in the window so it resizes every image looking ugly. I'd like to prevent the streching and keep the aspect ratio. Any clue?
This is the code of the proof of concept:
QApplication, QListWidget, QAbstractItemView, QPushButton, QWidget,
QTabWidget, QVBoxLayout, QHBoxLayout, QLabel, QPlainTextEdit,
QTextEdit, QCheckBox, QFileDialog, QMessageBox, QGridLayout from
PyQt5.QtCore import QProcess, QTimer, Qt from PyQt5.QtGui import QFont, QPixmap import os from QLabelClickable import QLabelClickable
class ExampleApp(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Example'
self.left = 0
self.top = 0
self.width = 1000
self.height = 800
self.setMaximumWidth(1000)
self.setMaximumHeight(800)
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.show()
data = []
for root, dirs, files in os.walk('img/'):
for file in files:
filepath = os.path.join(root, file)
data.append(filepath)
self.picture_window = PictureWindow(data=data)
self.picture_window.show()
class PictureWindow(QWidget):
def __init__(self, data):
super().__init__()
self.n_columns = 5
self.data = data
self.gridLayout = QGridLayout()
for i, filename in enumerate(self.data):
self.render_picture(filename=filename, pos=i)
self.setLayout(self.gridLayout)
def render_picture(self, filename, pos):
image_widget = QLabelClickable(pos)
image_widget.setGeometry(15, 15, 118, 130)
image_widget.setToolTip(filename)
image_widget.setCursor(Qt.PointingHandCursor)
self.pixmapImagen = QPixmap(filename).scaled(375, 375, Qt.KeepAspectRatio, Qt.SmoothTransformation)
image_widget.setPixmap(self.pixmapImagen)
image_widget.setAlignment(Qt.AlignCenter)
cell_layout = QVBoxLayout()
cell_layout.setAlignment(Qt.AlignHCenter)
cell_layout.addWidget(image_widget)
self.gridLayout.addLayout(cell_layout, int(pos / self.n_columns), pos % self.n_columns)
from PyQt5.QtCore import Qt, pyqtSignal, QTimer
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QMessageBox
class QLabelClickable(QLabel):
clicked = pyqtSignal(str)
def __init__(self, id, parent=None):
super(QLabelClickable, self).__init__(parent)
self.id = id
def mousePressEvent(self, event):
self.ultimo = "Click"
def mouseReleaseEvent(self, event):
if self.ultimo == "Click":
QTimer.singleShot(QApplication.instance().doubleClickInterval(),
self.performSingleClickAction)
else:
self.clicked.emit(self.ultimo+"-"+str(self.id))
def mouseDoubleClickEvent(self, event):
self.ultimo = "Double Click"
def performSingleClickAction(self):
if self.ultimo == "Click":
self.clicked.emit(self.ultimo+"-"+str(self.id))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ExampleApp()
sys.exit(app.exec_())
This is how it looks when window is normal:
And looks like this when window is maximized:
After a few days trying to figure out what's going on here I an't go further, could you please help me to spot anything wrong?
I expect the pics to keepe their aspect ratio to implement a ScrollArea later on.
I have tried to simulated below error but it is not consistent. may I know what does it really mean and how we can debug such issue.
The error generally occur from event function only
Error: elif self.review_waiver_box and obj is self.review_waiver_box.viewport():
RuntimeError: Internal C++ object (PySide2.QtWidgets.QTextEdit) already deleted.
import os
import sys
from PySide2.QtCore import QEvent, QSize, QRegularExpression
from PySide2.QtWidgets import (QPlainTextEdit, QApplication, QLabel,
QGridLayout, QGroupBox, QMessageBox,
QTextEdit, QCheckBox, QTableView,
QTableWidgetItem, QHeaderView, QWidget, QVBoxLayout,
QSizePolicy, QButtonGroup, QAbstractItemView,
QSplitter, QHBoxLayout, QComboBox)
from PySide2.QtWidgets import (QDesktopWidget, QMainWindow, QSizePolicy, QMessageBox, QToolBar, QAction, QLabel,
QMenu, QWidget, QVBoxLayout, QColorDialog, QDialog, QPushButton)
from functools import partial
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.width = 1100
self.height = 700
self.set_main_window()
self.create_violation_text()
def set_main_window(self):
"""
Setting main window position
"""
self.setWindowTitle("GUI %s" % (os.path.abspath(__file__)))
self.setFixedSize(QSize(self.width, self.height))
wid = QDesktopWidget()
screen_width = wid.screen().frameGeometry().width()
screen_height = wid.screen().frameGeometry().height()
self.setGeometry(screen_width / 2 - self.width / 2,
screen_height / 2 - self.height / 2,
self.width, self.height)
def create_violation_text(self):
"""
creating main violation window which contain all violations
"""
self.plain_textedit = QPlainTextEdit()
self.plain_textedit.setLineWrapMode(QPlainTextEdit.NoWrap)
self.plain_textedit.setStyleSheet(
"""QPlainTextEdit {font-size: 14pt;
font-family: Courier;}""")
self.plain_textedit.setReadOnly(True)
self.plain_textedit.setMouseTracking(True)
self.plain_textedit_format = self.plain_textedit.currentCharFormat()
self.plain_textedit.setCursorWidth(5)
self.plain_textedit.viewport().installEventFilter(self)
self.plain_textedit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
class ReviewWindow(MainWindow):
def __init__(self, app):
"""creating Review mode"""
super().__init__()
self.app = app
def create_widget(self):
self.waiver_approval_window = QGroupBox()
self.waiver_approval_layout = QGridLayout()
self.create_violation_window()
self.create_waiver_approval_list()
self.waiver_approval_window.setLayout(self.waiver_approval_layout)
# order matter
main_layout = QVBoxLayout()
main_layout.addWidget(self.create_violation_box)
main_layout.addWidget(self.waiver_approval_window)
# creating widgeth object as main window does not
# add layout directly it add only widget
window = QWidget()
window.setLayout(main_layout)
self.setCentralWidget(window)
def create_violation_window(self):
"""
creating violation window which contain text editor,
violation type and checkbox
"""
self.create_violation_box = QGroupBox()
self.large_file_batch_selection = QComboBox()
self.large_file_batch_selection.setGeometry(10, 15, 10, 15)
self.large_file_batch_selection.currentIndexChanged.connect(partial(self.load_range))
self.large_file_batch_selection.addItems(["0-1", "1-2"])
layout = QGridLayout()
layout.addWidget(self.large_file_batch_selection, 1, 0)
layout.addWidget(self.plain_textedit, 2, 0, 12, 1)
layout.setColumnMinimumWidth(0, 10)
self.create_violation_box.setLayout(layout)
def create_waiver_approval_list(self):
"""
Adding plain text to editor to window
"""
# waiver review pane
print("coming here")
self.waiver_file_path = QLabel()
self.waiver_file_path.setText("path")
# layout.addWidget(self.waiver_file_path, 0, 0)
self.waiver_approval_layout.addWidget(self.waiver_file_path, 0, 0)
font_family = """QTextEdit {font-size: 14pt;
background-color: #ffffe6;
color: #404040;
font-family: Courier;}"""
self.review_waiver_box = QTextEdit("Waiver_window")
self.review_waiver_box.setReadOnly(True)
self.review_waiver_box.setMouseTracking(True)
self.review_waiver_box.viewport().installEventFilter(self)
# layout.addWidget(self.review_waiver_box, 1, 0)
self.waiver_approval_layout.addWidget(self.review_waiver_box, 1, 0)
self.review_waiver_box.setPlainText("data")
# approval pane
self.approval_waiver_box = QTextEdit()
self.approval_file_path = QLabel()
self.approval_file_path.setText("path")
# layout.addWidget(self.approval_file_path, 0, 1)
self.waiver_approval_layout.addWidget(self.approval_file_path, 0, 1)
self.approval_waiver_box.setPlainText("data")
font_family = """QTextEdit {font-size: 14pt;
background-color: #ffd9b3;
color: #404040;
font-family: Courier;}"""
self.approval_waiver_box.setStyleSheet(font_family)
self.approval_waiver_box.setHidden(True)
self.approval_file_path.setHidden(True)
self.approval_waiver_box.setMouseTracking(True)
self.approval_waiver_box.setReadOnly(True)
self.approval_waiver_box.viewport().installEventFilter(self)
# layout.addWidget(self.approval_waiver_box, 1, 1)
# self.waiver_approval_window.setLayout(layout)
self.waiver_approval_layout.addWidget(self.approval_waiver_box, 1, 1)
def load_range(self, index):
print ("index")
def eventFilter(self, obj, event):
print ("event")
if obj is self.plain_textedit.viewport() and event.type() == QEvent.MouseButtonPress:
print ("text edit event")
elif self.review_waiver_box and obj is self.review_waiver_box.viewport():
if event.type() == QEvent.MouseButtonPress:
print ("button press")
return super().eventFilter(obj, event)
def closeEvent(self, event):
print ("pass")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ReviewWindow(app)
window.create_widget()
window.show()
app.exec_()
i would like to embed the glb file to pyqt gui with vtk python library. I wrote pieces of code but it does not embed the sketch to the pyqt gui. Everytime there have been placing in the second windows form. Here is the example:
And here is the my source code that i am trying to embed it to gui:
import sys
import vtk
try:
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtWidgets import QWidget, QSizePolicy, QPushButton, QVBoxLayout, QFrame
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSignal, QTimer, QObject, QSize, QEvent
except ImportError:
raise ImportError("Cannot load either PyQt5")
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
importer = vtk.vtkGLTFImporter()
importer.SetFileName("uydu.glb")
importer.Read()
global vtk_render_window
vtk_renderer = importer.GetRenderer()
vtk_render_window = importer.GetRenderWindow()
self.setGeometry(190, 300, 300, 200)
self.setWindowTitle('Simple menu')
self.show()
self.frame = QFrame()
self.vl = QVBoxLayout()
vtk_render_window_interactor = QVTKRenderWindowInteractor(self.frame)
vtk_render_window_interactor.SetRenderWindow(vtk_render_window)
colors = vtk.vtkNamedColors()
vtk_renderer.SetBackground(colors.GetColor3d('White'))
actor = vtk.vtkActor()
actor.GetProperty().SetDiffuse(0.8)
actor.GetProperty().SetDiffuseColor(colors.GetColor3d('Black'))
actor.GetProperty().SetSpecular(0.3)
actor.GetProperty().SetSpecularPower(60.0)
vtk_render_window.SetSize(600, 600)
vtk_render_window.SetWindowName('3D Visualizer')
vtk_render_window_interactor.Initialize()
# Add callback for getting data from Arduino
#vtk_render_window_interactor.CreateRepeatingTimer(1000)
#vtk_render_window_interactor.AddObserver("TimerEvent", information_callback)
global vtk_actors
vtk_actors = vtk_renderer.GetActors
self.vl.addWidget(vtk_render_window_interactor)
self.renderer = vtk.vtkRenderer()
self.renderer.SetBackground(.3, .4, .5 )
self.renwin = vtk_render_window_interactor.GetRenderWindow()
self.renwin.AddRenderer(self.renderer)
# An interactor
self.inter = self.renwin.GetInteractor()
# add the custom style
self.style = MouseInteractorHighLightActor()
self.style.SetDefaultRenderer(self.renderer)
vtk_render_window_interactor.GetRenderWindow().AddRenderer(self.renderer)
#self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.inter.SetInteractorStyle(self.style)
#self.iren.SetInteractorStyle(self.inter)
#self.iren = self.vtkWidget.GetRenderWindow().SetInteractor(self.inter)
self.renderer.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.inter.Initialize()
self.inter.Start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
You have to pass the renderWindow of vtkGLTFImporter as rw parameter to the QVTKRenderWindowInteractor widget:
import sys
import vtk
try:
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtWidgets import QWidget, QVBoxLayout
except ImportError:
raise ImportError("Cannot load either PyQt5")
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(190, 300, 300, 200)
self.setWindowTitle("Simple menu")
self.container = QWidget()
vl = QVBoxLayout(self.container)
self.setCentralWidget(self.container)
self.resize(640, 480)
importer = vtk.vtkGLTFImporter()
importer.SetFileName("uydu.glb")
importer.Read()
renderer = importer.GetRenderer()
render_window = importer.GetRenderWindow()
vtk_widget = QVTKRenderWindowInteractor(rw=render_window)
vl.addWidget(vtk_widget)
vtk_widget.Initialize()
vtk_widget.Start()
colors = vtk.vtkNamedColors()
renderer.SetBackground(colors.GetColor3d("White"))
renderer.ResetCamera()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Menu()
ex.show()
sys.exit(app.exec_())
This question already has answers here:
How do I assert the identity of a PyQt5 signal?
(2 answers)
Closed 2 years ago.
I've created a search engine in PyQt5, using the code below:
import sys
from PyQt5.QtWidgets import (
QWidget, QLineEdit, QLabel, QScrollArea, QMainWindow,
QApplication, QHBoxLayout, QVBoxLayout, QSpacerItem, QSizePolicy, QCompleter, QPushButton
)
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
tlist = ['thing1', 'thing2', 'thing3', 'thing4']
class Label(QWidget):
def __init__(self, name):
super(Label, self).__init__()
self.name = name
self.lbl = QLabel(self.name)
self.lbl.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
self.btn = QPushButton("Preview")
self.btn.setMaximumSize(QtCore.QSize(100,100))
self.btn.clicked.connect(self.printsignal)
self.hbox = QHBoxLayout()
self.hbox.addWidget(self.lbl)
self.hbox.addWidget(self.btn)
self.setLayout(self.hbox)
def show(self):
for labels in [self, self.lbl]:
labels.setVisible(True)
def hide(self):
for labels in [self, self.lbl]:
labels.setVisible(False)
def printsignal(self):
print("clicked")
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__()
self.controls = QWidget()
self.controlsLayout = QVBoxLayout()
self.widgets = []
for name in tlist:
item = Label(name)
self.controlsLayout.addWidget(item)
self.widgets.append(item)
spacer = QSpacerItem(1, 1, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.controlsLayout.addItem(spacer)
self.controls.setLayout(self.controlsLayout)
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.controls)
self.searchbar = QLineEdit()
self.searchbar.textChanged.connect(self.update_display)
self.completer = QCompleter(tlist)
self.completer.setCaseSensitivity(Qt.CaseInsensitive)
self.searchbar.setCompleter(self.completer)
container = QWidget()
containerLayout = QVBoxLayout()
containerLayout.addWidget(self.searchbar)
containerLayout.addWidget(self.scroll)
container.setLayout(containerLayout)
self.setCentralWidget(container)
self.setGeometry(600, 100, 800, 600)
self.setWindowTitle('Search Engine')
def update_display(self, text):
for widget in self.widgets:
if text.lower() in widget.name.lower():
widget.show()
else:
widget.hide()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
The problem I have is, all the buttons share the same function and I don't know how to make them have different signals, as they are generated automatically. Basically, if I run the code it will show up like
this:
and when I press any of the buttons, it will print "clicked" (as in printsignal function). What I want is a different function for each button. Is there a way to do that?
Normally you can use self.sender().text() to get text from QButton which generated signal.
But because you create own widget Label with QButton and QLabel and you want text from label so you can get directly self.name
def printsignal(self):
print("clicked", self.name)
eventually self.lbl.text()
def printsignal(self):
print("clicked", self.lbl.text())
Working code.
I removed show(), hide() because you don't need it
import sys
from PyQt5.QtWidgets import (
QWidget, QLineEdit, QLabel, QScrollArea, QMainWindow,
QApplication, QHBoxLayout, QVBoxLayout, QSpacerItem, QSizePolicy, QCompleter, QPushButton
)
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
tlist = ['thing1', 'thing2', 'thing3', 'thing4']
class Label(QWidget):
def __init__(self, name):
super().__init__()
self.name = name
self.lbl = QLabel(self.name)
self.lbl.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
self.btn = QPushButton("Preview")
self.btn.setMaximumSize(QtCore.QSize(100,100))
self.btn.clicked.connect(self.printsignal)
self.hbox = QHBoxLayout()
self.hbox.addWidget(self.lbl)
self.hbox.addWidget(self.btn)
self.setLayout(self.hbox)
def printsignal(self):
print("clicked", self.name)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__()
self.controls = QWidget()
self.controlsLayout = QVBoxLayout()
self.widgets = []
for name in tlist:
item = Label(name)
self.controlsLayout.addWidget(item)
self.widgets.append(item)
spacer = QSpacerItem(1, 1, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.controlsLayout.addItem(spacer)
self.controls.setLayout(self.controlsLayout)
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.controls)
self.searchbar = QLineEdit()
self.searchbar.textChanged.connect(self.update_display)
self.completer = QCompleter(tlist)
self.completer.setCaseSensitivity(Qt.CaseInsensitive)
self.searchbar.setCompleter(self.completer)
container = QWidget()
containerLayout = QVBoxLayout()
containerLayout.addWidget(self.searchbar)
containerLayout.addWidget(self.scroll)
container.setLayout(containerLayout)
self.setCentralWidget(container)
self.setGeometry(600, 100, 800, 600)
self.setWindowTitle('Search Engine')
def update_display(self, text):
for widget in self.widgets:
if text.lower() in widget.name.lower():
widget.show()
else:
widget.hide()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
What I'm trying to do is when a user (via a touchscreen) clicks on an editable QEditLine I want it to show the Matchbox-Keyboard for user input. When it is not clicked do not show the keyboard.
I've gone through the C documentation, and a few C examples, but I'm lost as too make the jump to Python. I see people mentioning setting the "focus" can someone explain this too me?
import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QSlider, QComboBox, QCheckBox, QWidget, QMainWindow, QPushButton, QLabel, QGridLayout, QGroupBox, QRadioButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot, Qt
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'GUI TESTS'
self.left = 0
self.top = 0
self.width = 800
self.height = 400
self.statusBarMessage = "GUI TEST"
self.currentSprite = 'TEST.png'
self.btn1Active = False
self.btn2Active = False
self.btn3Active = False
self.btn4Active = False
self.btn5Active = False
self.btn6Active = False
self.btn7Active = False
self.btn8Active = False
self.saveLocationDir = ""
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage(self.statusBarMessage)
self.userNameLabel = QLabel(self)
self.userNameLabel.move(0,125)
self.userNameLabel.setText("What is your name?")
self.userNameLabel.resize(120,20)
self.nameInput = QLineEdit(self)
self.nameInput.move(0,145)
self.nameInput.resize(200,32)
self.nameInput.setEchoMode(0)
#pyqtSlot()
def showKeyboard(self):
command = "matchbox-keyboard"
os.system(command)
It is not recommended to override the events method by assigning a function self.nameInput.mousePressEvent = self.showKeyboard since the tasks of the mousePressEvent of the QLineEdit are lost and could cause unexpected events.
Also, mousePressEvent is not the appropriate event since you can press the QLineEdit many times and it would be called back to the keyboard.
A better option is to launch it in focusInEvent and delete it in focusOutEvent:
import sys
import subprocess
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MatchBoxLineEdit(QLineEdit):
def focusInEvent(self, e):
try:
subprocess.Popen(["matchbox-keyboard"])
except FileNotFoundError:
pass
def focusOutEvent(self,e):
subprocess.Popen(["killall","matchbox-keyboard"])
class App(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('GUI TESTS')
widget = QWidget()
self.setCentralWidget(widget)
lay = QVBoxLayout(widget)
self.userNameLabel = QLabel("What is your name?")
self.nameInput = MatchBoxLineEdit()
lay.addWidget(self.userNameLabel)
lay.addWidget(self.nameInput)
self.setGeometry(
QStyle.alignedRect(
Qt.LeftToRight,
Qt.AlignCenter,self.sizeHint(),
qApp.desktop().availableGeometry()
)
)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec_())
import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QSlider, QComboBox, QCheckBox, QWidget, QMainWindow, QPushButton, QLabel, QGridLayout, QGroupBox, QRadioButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot, Qt
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'GUI TESTS'
self.left = 0
self.top = 0
self.width = 800
self.height = 400
self.statusBarMessage = "GUI TEST"
self.currentSprite = 'TEST.png'
self.btn1Active = False
self.btn2Active = False
self.btn3Active = False
self.btn4Active = False
self.btn5Active = False
self.btn6Active = False
self.btn7Active = False
self.btn8Active = False
self.saveLocationDir = ""
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage(self.statusBarMessage)
self.userNameLabel = QLabel(self)
self.userNameLabel.move(0,125)
self.userNameLabel.setText("What is your name?")
self.userNameLabel.resize(120,20)
self.nameInput = QLineEdit(self)
self.nameInput.move(0,145)
self.nameInput.resize(200,32)
self.nameInput.setEchoMode(0)
self.nameInput.mousePressEvent=self.showKeyboard
#pyqtSlot()
def showKeyboard(self,event):
if event.button() == QtCore.Qt.LeftButton:
QtWidgets.QLineEdit.mousePressEvent(self, event)
command = "matchbox-keyboard"
os.system(command)
You can override mousePressEvent and achieve that functionality