How to access first_file.py methods (eventfilter and func_textbox_textchanged methods) from second_file.
In my first_file.py, I have a methods eventfilter and func_textbox_textchanged. Now I want to access the methods from my second File. paste my code here. nothing Will happen as desired.
first_file.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Label():
def __init__(self,textbox):
print("inside constructor")
self.tbox1 = textbox
def eventFilter(self, source,event):
print("inside event filter")
if event.type() == QEvent.KeyPress and source is self.tbox1:
if event.key() == Qt.Key_A:
pritn("Key A is pressed")
return super(label, self).eventFilter(source, event)
def func_textbox_textchanged(self,txt):
print("inside text box text changed")
print(tbox1.text())
second_file.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from first_file import *
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox = QLineEdit(self)
self.textbox.setGeometry(100,100,300,30)
self.textbox.installEventFilter(self)
process = label(self.textbox)
self.textbox.textChanged.connect(process.func_textbox_textchanged)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == '__main__':
main()
You have several typos but even correcting them does not solve your underlying problem since it is conceptual and those problems are the following:
Only QObjects can be an event filter, but Label is not.
Local variables are destroyed and in your case "process" it is so if you have to extend its cycle, and for example you can take advantage of monitoring other widget processes so it is enough that it has the same scope, so we can use that other widget like parent.
To install an event filter, use X.installEventFilter(Y) that can be read as X will send its events to Y or that Y will listen to the events of X, but in your self.textbox.installEventFilter(self) it does not make sense since it is not related to your requirement.
I will obviously point out where the numerous typos are as they are trivial.
Considering the above, the solution is:
from PyQt5.QtCore import QEvent, QObject, Qt
class Label(QObject):
def __init__(self, textbox):
super().__init__(textbox)
print("inside constructor")
self.tbox1 = textbox
self.tbox1.installEventFilter(self)
def eventFilter(self, source, event):
print("inside event filter")
if event.type() == QEvent.KeyPress and source is self.tbox1:
if event.key() == Qt.Key_A:
print("Key A is pressed")
return super(Label, self).eventFilter(source, event)
def func_textbox_textchanged(self, txt):
print("inside text box text changed")
print(self.tbox1.text())
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget
from first_file import Label
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox = QLineEdit(self)
self.textbox.setGeometry(100, 100, 300, 30)
process = Label(self.textbox)
self.textbox.textChanged.connect(process.func_textbox_textchanged)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With