Signals in Pyside6 - python

I can't figure out why the signals don't work. In PyQt5, this code worked (the difference was that instead of Signal, it was pyqtSignal).
When you click on the button, TextEdit should display the message "connecting to the device", if you replace pyside with pyqt, the code will work as it should
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(188, 267)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(50, 140, 75, 24))
self.textEdit = QTextEdit(self.centralwidget)
self.textEdit.setObjectName(u"textEdit")
self.textEdit.setGeometry(QRect(30, 40, 104, 71))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.dragPos = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
def update_text(value, textEdit):
textEdit.setText(textEdit.toPlainText() + value)
textEdit.verticalScrollBar().setValue(textEdit.verticalScrollBar().maximum())
class account(QThread):
textUpdate = Signal(str, QTextEdit)
def __init__(self):
super().__init__(parent=None)
self.textUpdate.connect(update_text)
def run(self):
print("thread is work")
self.textUpdate.emit("Connect to device\n", ui.textEdit)
if __name__ == "__main__":
app = QApplication()
acc_instance = account()
main = MainWindow()
ui = main.ui
ui.pushButton.clicked.connect(acc_instance.start)
sys.exit(app.exec_())
P.S. I know that override run method is incorrect.
P.S.S Added a small example

Your code has several problems:
The scope of "ui" is limited, it is not a global variable so you cannot use it in the run method.
Only some data types are registered (which can only be done in C++) so that they can be sent through signals, and that is not the case with QTextEdit. A workaround is to look for a parent class of the QTextEdit that is registered as a QObject or an object.
But in this case I do not see the need to send QTextEdit but only the data and then modify the GUI to set it since that is its task.
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#Slot(str)
def update_text(self, value):
self.ui.textEdit.setText(self.ui.textEdit.toPlainText() + value)
self.ui.textEdit.verticalScrollBar().setValue(
self.ui.textEdit.verticalScrollBar().maximum()
)
class Account(QThread):
textUpdate = Signal(str)
def run(self):
print("thread is work")
self.textUpdate.emit("Connect to device\n")
if __name__ == "__main__":
app = QApplication()
main = MainWindow()
acc_instance = Account()
acc_instance.textUpdate.connect(main.update_text)
main.ui.pushButton.clicked.connect(acc_instance.start)
main.show()
sys.exit(app.exec_())
Note: In pyqt6 your initial code doesn't work either.
If you want to send texts to several QTextEdit then it is better to create a key that associates each type of text to a QTextEdit group:
from collections import defaultdict
from functools import cached_property
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, parent=None)
self.dragPos = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.register("device_viewer", self.ui.textEdit)
# self.register("another_key", another_textedit)
def register(self, key, textedit):
if not isinstance(textedit, QTextEdit):
raise TypeError(f"{textedit} must be a QTextEdit")
self.registry_viewers[key].append(textedit)
#cached_property
def registry_viewers(self):
return defaultdict(list)
#Slot(str, str)
def update_text(self, key, value):
for textedit in self.registry_viewers[key]:
textedit.setText(textedit.toPlainText() + value)
textedit.verticalScrollBar().setValue(
textedit.verticalScrollBar().maximum()
)
class Account(QThread):
textUpdate = Signal(str, str)
def run(self):
print("thread is work")
self.textUpdate.emit("device_viewer", "Connect to device\n")
# self.textUpdate.emit("another_key", "message")
if __name__ == "__main__":
app = QApplication()
main = MainWindow()
acc_instance = Account()
acc_instance.textUpdate.connect(main.update_text)
main.ui.pushButton.clicked.connect(acc_instance.start)
sys.exit(app.exec_())

Related

How to set widget for vispy use

I know this question has been asked in a couple ways but I cannot figure it out on my specific use case. I used QT designer (pyQT 5.15) to create a layout and would like to use vispy as the display. After reading it seemed I had to set a widget but it still isn't clear to me how. This is what I have, and I am trying to have the simple example of changing the color from black to white via a timer. There are no errors but there is no visualization either.
Here is the GUI.ui converted to Python called GUI.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(702, 603)
self.vispy_widget = QtWidgets.QWidget(Dialog)
self.vispy_widget.setGeometry(QtCore.QRect(150, 20, 531, 531))
self.vispy_widget.setObjectName("vispy_widget")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(580, 560, 75, 23))
self.pushButton.setObjectName("pushButton")
self.horizontalScrollBar = QtWidgets.QScrollBar(Dialog)
self.horizontalScrollBar.setGeometry(QtCore.QRect(160, 560, 401, 20))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(20, 60, 111, 16))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.hist_widget = QtWidgets.QWidget(Dialog)
self.hist_widget.setGeometry(QtCore.QRect(20, 90, 120, 80))
self.hist_widget.setObjectName("hist_widget")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
```
Here is the app part:
import sys
from vispy import gloo, app
from gui import Ui_Dialog
from PyQt5 import QtWidgets
app.use_app('pyqt5')
class Canvas(app.Canvas):
def __init__(self, *args, **kwargs):
app.Canvas.__init__(self, *args, **kwargs)
self._timer = app.Timer('auto', connect=self.on_timer, start=True)
self.tick = 0
def on_draw(self, event):
gloo.clear(color=True)
def on_timer(self, event):
self.tick += 1 / 60.0
c = abs(math.sin(self.tick))
gloo.set_clear_color((c, c, c, 1))
self.update()
class myWindow(QtWidgets.QMainWindow):
def __init__(self):
super(myWindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
canvas = Canvas()
self.ui.vispy_widget(canvas.native)
if __name__ == "__main__":
gui = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
app.run()
#sys.exit(app.exec_())
There are the following errors:
The self.ui.vispy_widget(canvas.native) command does not make sense, the idea is to use vispy_widget as a container for the native vispy widget that can be placed through a layout.
The choice of the .ui form is used to determine the base class, in your case you should use QDialog instead of QMainWindow.
If you already set the Ui_Dialog in the widget then it is unnecessary to implement the same in if __name__ == "__main__":.
You must import the math module.
import math
import sys
from vispy import gloo, app
from PyQt5 import QtWidgets
from gui import Ui_Dialog
app.use_app("pyqt5")
class Canvas(app.Canvas):
def __init__(self, *args, **kwargs):
app.Canvas.__init__(self, *args, **kwargs)
self._timer = app.Timer("auto", connect=self.on_timer, start=True)
self.tick = 0
def on_draw(self, event):
gloo.clear(color=True)
def on_timer(self, event):
self.tick += 1 / 60.0
c = abs(math.sin(self.tick))
gloo.set_clear_color((c, c, c, 1))
self.update()
class MyWindow(QtWidgets.QDialog):
def __init__(self):
super(MyWindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.canvas = Canvas()
lay = QtWidgets.QVBoxLayout(self.ui.vispy_widget)
lay.addWidget(self.canvas.native)
if __name__ == "__main__":
gui = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
app.run()

updating a label value on the GUI which gets updated value from temperature sensor all the time

my project is to design a GUI window to get Temperature from temp sensor and display it on a label widget on the GUI. i was able to display the value but it's not updating on the label
please help. i was looking about how to emit signal in pyqt5 and connect it to the label widget
from PyQt5 import QtCore, QtGui, QtWidgets
from w1thermsensor import W1ThermSensor
from PyQt5.QtCore import Qt, QThread, pyqtSignal, pyqtSlot
sensor = (W1ThermSensor())
class Ui_Form(object):
def setupUi(self, parent=None):
Form.setObjectName("Form")
Form.resize(400, 300)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 40, 141, 71))
self.label.setObjectName("label")
self.label.setNum(int(sensor.get_temperature()))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.threadclass = ThreadClass()
self.threadclass.start()
self.signals(self)
def signals(self):
self.threadclass.connect(self.updateLabel)
self.threadclass.connect(pyqtSignal(val))
self.pyqtSignal(val).connect(self.updateLabel)
def updateLabel(self,val):
val = int(sensor.get_temperature())
self.label.setNum(val)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
class ThreadClass(QtCore.QThread):
change_value = pyqtSignal(int)
def setupUi(self, parent=None):
super(ThreadClass, self).__init__(parent)
def run(self):
while 1:
val = int(sensor.get_temperature())
self.change_value.emit(val)
print (val)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
ui.signals()
Form.show()
sys.exit(app.exec_())
There are various problems with your code:
you are calling self.signals(self), but signals does not accept arguments;
connections are between signals and slots, but you're trying to connect to the threadclass instance, which is neither a signal nor a slot, but a QThread object;
self.threadclass.connect(pyqtSignal(val)) does not make much sense: pyqtSignal is a function used when a class is constructed, and can only be used as such; also, val is never declared in the scope of signals();
self.pyqtSignal(val).connect(self.updateLabel) doesn't work either: self is the Ui_Form instance, and has no pyqtSignal attribute; again, no val is declared;
the python files generated with pyuic should never be edited, but used as imported modules only and all program implementation should happen in another class, not in the "ui" objects it creates (read more about using Designer);
Recreate the GUI if you don't have it anymore, and generate again the ui with pyuic. Then try to do something like this (supposing you've created a file named ui_form.py) in a separate file:
from PyQt5 import QtCore, QtWidgets
from ui_form import Ui_Form
class MyTest(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(MyTest, self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.change_value.connect(self.updateLabel)
self.threadclass.start()
def updateLabel(self, value):
self.label.setNum(value)
class ThreadClass(QtCore.QThread):
change_value = pyqtSignal(int)
def setupUi(self, parent=None):
super(ThreadClass, self).__init__(parent)
def run(self):
while 1:
val = int(sensor.get_temperature())
# I suppose
self.change_value.emit(val)
print (val)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
test = MyTest
test.show()
sys.exit(app.exec_())

PyQt5 passing argument between two classes: lambda vs partial

I am trying to pass an argument between two PyQt5 classes. I used three methods:
Using lambda functions.
Wrapper function (similar to lambda function).
partial from functools module.
In the example below, I have two windows:
MainWindow has QLineEdit mw_line_edit and a QPushButton mw_open_new_dialog_button.
Dialog: has a QLineEdit line_edit and aQPushButton push_button.
When I click the button push_button, I want it to insert the content of line_edit to mw_line_edit.
Here is a minimal example:
import sys
from functools import partial
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.central_widget)
self.mw_open_new_dialog_button = QtWidgets.QPushButton('Open New dialog', self)
self.mw_line_edit = QtWidgets.QLineEdit(self)
self.hlayout = QtWidgets.QHBoxLayout(self)
self.hlayout.addWidget(self.mw_open_new_dialog_button)
self.hlayout.addWidget(self.mw_line_edit)
self.central_widget.setLayout(self.hlayout)
self.mw_open_new_dialog_button.clicked.connect(self.open_new_dialog)
def open_new_dialog(self):
self.dlg = Dialog()
#self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit.text())) # <<<<<<< This does not work
self.dlg.clicked.connect(lambda: self.write_something(self.dlg.line_edit.text())) # this works
#self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit.text()))# <<<<<<<<<<This does not work
self.dlg.exec()
#QtCore.pyqtSlot()
def write_something(self, text):
self.mw_line_edit.setText(text)
def wrapper(self, text):
return lambda: self.write_something(text)
class Dialog(QtWidgets.QDialog):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(QtWidgets.QDialog, self).__init__(parent)
self.hlayout = QtWidgets.QHBoxLayout(self)
self.line_edit = QtWidgets.QLineEdit(self)
self.push_button = QtWidgets.QPushButton('Click me', self)
self.hlayout.addWidget(self.line_edit)
self.hlayout.addWidget(self.push_button)
self.label = QtWidgets.QLabel('I am a Qlabel', self)
self.hlayout.addWidget(self.label)
self.setLayout(self.hlayout)
self.push_button.clicked.connect(self.clicked)
def write_something(self, text):
print(text)
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
As you can see in the commented lines, only the following method works:
self.dlg.clicked.connect(lambda: self.write_something(self.dlg.line_edit.text()))
Why the other two do not work, i.e:
self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit.text())) # <<<<<<< This does not work
self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit.text()))# <<<<<<<<<<This does not work
Thanks
1) functools.partial()
What arguments are you passing to partial? You are passing the method write_something and the text of self.dlg.line_edit at the time the connection is made.
And what is the value of that text? it is an empty string, this explains the failure.
Is there any solution for this case? Yes, instead of passing the text, pass the QLineEdit, and in the method write_something get the text and set it in the other QLineEdit:
def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.clicked.connect(partial(self.write_something, self.dlg.line_edit))
self.dlg.exec()
def write_something(self, le):
self.mw_line_edit.setText(le.text())
2) wrapper
It is the same problem, you are passing the empty text that you have at the moment of the connection
Is there any solution? Yes, the same solution as the previous one.
def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.clicked.connect(self.wrapper(self.dlg.line_edit))
self.dlg.exec()
def write_something(self, text):
self.mw_line_edit.setText(text)
def wrapper(self, line):
return lambda: self.write_something(line.text())
Will there be a clean solution? Yes, create a signal that transports the text when you click.
from PyQt5 import QtWidgets, QtGui, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.mw_open_new_dialog_button = QtWidgets.QPushButton('Open New dialog')
self.mw_line_edit = QtWidgets.QLineEdit()
hlayout = QtWidgets.QHBoxLayout(central_widget)
hlayout.addWidget(self.mw_open_new_dialog_button)
hlayout.addWidget(self.mw_line_edit)
self.mw_open_new_dialog_button.clicked.connect(self.open_new_dialog)
#QtCore.pyqtSlot()
def open_new_dialog(self):
self.dlg = Dialog()
self.dlg.textSignal.connect(self.mw_line_edit.setText)
self.dlg.exec()
class Dialog(QtWidgets.QDialog):
textSignal = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(QtWidgets.QDialog, self).__init__(parent)
hlayout = QtWidgets.QHBoxLayout(self)
self.line_edit = QtWidgets.QLineEdit()
self.push_button = QtWidgets.QPushButton('Click me')
hlayout.addWidget(self.line_edit)
hlayout.addWidget(self.push_button)
self.label = QtWidgets.QLabel('I am a Qlabel')
hlayout.addWidget(self.label)
self.push_button.clicked.connect(self.sendText)
#QtCore.pyqtSlot()
def sendText(self):
self.textSignal.emit(self.line_edit.text())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())

How to access a widget in a QMainWindow from a QDialog

Before posting my question I searched a lot about it and I found some questions that might be similar but they do not solve my problem. I believe it is quite easy but I don't know how:
below is a minimal example of the problem:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.firstPushButton.clicked.connect(self.showDialog)
# the other stuff related to layout setup is ommited
def showDialog(self):
dialog = MyDialog(MainWindow)
dialog.exec()
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.writeHello)
def writeHello(self):
# How can I access firstLineEdit from MainWindow? I want to write "Hello" to the firstLineEdit
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setWindowTitle("BEM Analysis for propellers")
MainWindow.show()
sys.exit(app.exec())
Could you please tell me how can I implement writeHello() method in order to write something in firstLineEdit in the MainWindow
Thanks
First of all you should not modify the code generated by Qt Designer since it is not a GUI, it is just a class that fills a GUI, and that brings several inconveniences such as not being able to overwrite the methods of the widget, or some you want to use methods of the widget in that class. Instead it inherits from a suitable widget and uses the other class as an interface.
Going to the point you should not mix the classes since there will be a lot of dependency between them and in the future if you modify one class you will have to modify the other which is unbeatable, instead you use the signals to notify any change or action.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
MainWindow.setWindowIcon(QtGui.QIcon(':/icons/drone.ico'))
MainWindow.setIconSize(QtCore.QSize(32, 32))
self.centralwidget = QtWidgets.QWidget(MainWindow)
MainWindow.setCentralWidget(self.centralwidget)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.verticalLayout_2.addWidget(self.firstPushButton)
self.verticalLayout_2.addWidget(self.firstLineEdit)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.firstPushButton.clicked.connect(self.showDialog)
def showDialog(self):
dialog = MyDialog()
dialog.clicked.connect(self.writeHello)
dialog.exec()
#QtCore.pyqtSlot()
def writeHello(self):
self.firstLineEdit.setText('Hello')
class MyDialog(QtWidgets.QDialog):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.clicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.myButton)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.setWindowTitle("BEM Analysis for propellers")
w.show()
sys.exit(app.exec())

PYQT5 crashing when calling .text() on a QLineEdit widget

When I run the code below my program crashes, and I believe this is to-do with the .text() called when the Line edit has something typed in it. I need to assign a variable to what is entered here.
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
usernameBox = QtWidgets.QLineEdit()
usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = usernameBox.text()
print(x)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = loginScreen()
sys.exit(app.exec_())
If you observe usernameBox it is created as a local variable so it can not be accessed by the other methods of the class, in your case there are 2 solutions:
Make a usernameBox attribute of the class.
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.usernameBox = QtWidgets.QLineEdit()
self.usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(self.usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = self.usernameBox.text()
print(x)
Or use sender() that obtains the object that emits the signal, in your case the QLineEdit.
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
usernameBox = QtWidgets.QLineEdit()
usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = self.sender().text()
print(x)

Categories