How to add a QInputDialog in pyqt5 and get its value - python

from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
self.label.setObjectName("label")
self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
self.lineedit.setObjectName("lineedit")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "label1:"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I want to have a QInputDialog under my lineedit, so I can choose from "yes" or "no"from the input dialog, how to I put it into my code?
ALso how do I get the value of the inputdialog?
Thanks!

Try it:
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 40, 81, 20))
self.label.setObjectName("label")
self.lineedit = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit.setGeometry(QtCore.QRect(270, 40, 113, 20))
self.lineedit.setObjectName("lineedit")
# +++
self.btn = QtWidgets.QPushButton(self.centralwidget)
self.btn.setGeometry(QtCore.QRect(200, 70, 81, 20))
self.btn.setObjectName("btn")
self.btn.clicked.connect(self.getTextInputDialog)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "label1:"))
self.btn.setText(_translate("MainWindow", "InputDialog"))
# +++
def getTextInputDialog(self):
text, okPressed = QtWidgets.QInputDialog.getText(None,
"Get text",
"Your name:",
QtWidgets.QLineEdit.Normal,
"")
if okPressed and text != '':
print(text)
ui.lineedit.setText(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Related

Msg_box after clicking button in PyQT

I want to get MsgBox after button click. I try to use PyQt5 for it. I make windows form with one button:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 330, 251, 51))
self.pushButton.setObjectName("PushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Then I try to enter button clicked function:
def buttonClicked(self):
msg = QMessageBox()
msg.setText("This is a message box")
But it doesn't work at all. How should I spent my problem?
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 330, 251, 51))
self.pushButton.setObjectName("PushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.buttonClicked) # +++
def buttonClicked(self):
msg = QtWidgets.QMessageBox.information(
self,
'Information',
'This is a message box')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
You have done well to create the QMessageBox but not shown them, so a possible solution is to use the exec_() method:
def buttonClicked(self):
msg = QMessageBox()
msg.setText("This is a message box")
msg.exec_()

I'm trying to make my PyQt GUI code in OOP way

I'm trying to make my PyQt5 GUI in OOP way for example class for window and another for Pushbutton I'm really confiuse how to make it.Here is my code and my try to make pushbutton class.also my goal is to make classes for each of the items in the window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#making Pushbutton
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
# here I'm trying to make Buttom class
class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
# Main function
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from Subclassing of QPushButton :
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 6 16:21:24 2020
#author: Pietro
"""
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#making Pushbutton
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pippo=CustomButtonClass(self.centralwidget)
self.pippo.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
# here I'm trying to make Buttom class
class CustomButtonClass(QtWidgets.QPushButton):
def __init__(self, *args, **kwargs):
QtWidgets.QPushButton.__init__(self,*args, **kwargs)
self.setGeometry(QtCore.QRect(280, 310, 93, 28))
self.setText('customized')
self.hide()
self.setStyleSheet("QPushButton{\n"
" background-color: #9de650;\n"
"}\n"
"\n"
"\n"
"QPushButton:hover{\n"
" background-color: green;\n"
"}\n"
"\n"
"")
# Main function
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

How to connect 3 different programs in one main file using pyqt5?

I have 3 programs here, and I have a problem in running those 3 programs because I want it to be all imported to the main file and connected to each other after clicking the buttons on each window. My problem here is that after I have reached the 2nd window, when I click the button there then it doesn't proceed to the last window but instead the window crashes. What would I change in my program so that it will not crash?
s1.py (main program)
from PyQt5 import QtCore, QtGui, QtWidgets
from s4 import *
from s3 import *
class Ui_MainWindow(object):
def ok (self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_MainWindow2()
self.ui.setup(self.window)
self.window.show()
MainWindow.hide()
def proceed (self):
self.window1=QtWidgets.QMainWindow()
self.ui1=Ui_MainWindow3()
self.ui1.setup1(self.window1)
self.window1.show()
MainWindow2.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(330, 220, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.ok)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
s4.py (2nd program)
from PyQt5 import QtCore, QtGui, QtWidgets
from s3 import *
class Ui_MainWindow2(object):
def proceed (self):
self.window1=QtWidgets.QMainWindow()
self.ui1=Ui_MainWindow3()
self.ui1.setup1(self.window1)
self.window1.show()
MainWindow2.hide()
def setup(self, MainWindow2):
MainWindow2.setObjectName("MainWindow2")
MainWindow2.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow2)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 270, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.proceed)
MainWindow2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow2)
self.statusbar.setObjectName("statusbar")
MainWindow2.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow2)
QtCore.QMetaObject.connectSlotsByName(MainWindow2)
def retranslateUi(self, MainWindow2):
_translate = QtCore.QCoreApplication.translate
MainWindow2.setWindowTitle(_translate("MainWindow2", "MainWindow"))
self.pushButton.setText(_translate("MainWindow2", "PROCEED"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow2 = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setup(MainWindow2)
MainWindow2.show()
sys.exit(app.exec_())
s3.py(3rd program)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow3(object):
def setup1(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 190, 451, 181))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "THIS IS MY LAST WINDOW"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow3()
ui.setup1(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
from s4 import Ui_MainWindow2
class Ui_MainWindow(object):
def ok (self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_MainWindow2()
self.ui.setup(self.window)
self.window.show()
MainWindow.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(330, 220, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ok)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
and import Ui_MainWindow3 from s3.py into s4 file and call proceed function as well as:
from s3 import Ui_MainWindow3

PyQt5 focusing on readonly QLineEdit widgets

I have 3 QLineEdit widgets that are readonly and listen to keyevents. Well I want to let the user to choose what QLineEdit he is going to use(because it's going to be a shortcut setting window). It appears when in readonly mode the widget is non-clickable. Is there a way to make it clickable or do the same thing without readonly?
I made the code in Qt Designer.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent1
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent2
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent3
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent1(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
def newOnkeyPressEvent2(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_2.setText(str(keyname))
def newOnkeyPressEvent3(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_3.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You need to determine which lineedit is under focus with self.centralwidget.focusWidget(). Here is full working code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
lineEdit = self.centralwidget.focusWidget()
lineEdit.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

PyQt5 how to show keyinput in QLineEdit? [duplicate]

This question already has an answer here:
How to record a pressed key combination in the PyQT5 dialog window
(1 answer)
Closed 3 years ago.
I am trying to get a readonly QLineEdit in a second window to show the name of the key pressed. For example if I press key a it shows a Key and if I press the spacebar it shows SpaceBar Key. I searched the net for solutions but most of them were about how to close a window when a certain key is pressed.
I have a working sample made with tkinter linked below.
https://gist.github.com/sangonkim/e397c420d93485f3896f9440bbcc77aa
I made 2 simple windows with QDesigner for the question.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(347, 259)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
self.pushButton.clicked.connect(self.connect)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def connect(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "To next window"))
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 261)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main_Window = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Main_Window)
Main_Window.show()
sys.exit(app.exec_())
I've tried adding
def keyinput(self, event):
print(event.key())
to Ui_MainWindow2see if I can capture the key I an typing. But it does not work.
And also tried
def keyinput(self, event):
self.lineEdit.setText(str(event.key()))
And failed.
into setupUi add:
MainWindow.keyPressEvent = self.newOnkeyPressEvent
and define function:
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
full code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(347, 259)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 110, 121, 28))
self.pushButton.clicked.connect(self.connect)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def connect(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "To next window"))
class Ui_MainWindow2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 261)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 100, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Main_Window = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Main_Window)
Main_Window.show()
sys.exit(app.exec_())

Categories

Resources