Hai, I want to print the value of variable, which is declared inside the "eventFilter" method ?. In my code, if focusitem is textbox1 then fitem = "textbox 1", if focus changes to textbox 2 then fitem is "textbox2" and so on . Now I want to print the fitem outside the method. ( For example, In my code Line 42 to 44, tried something, but nothing will happen)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
item1 = ["Python", "Python 2.7", "Python 2.9", "Python 3.5", "Python 3.7", "National", "Zebra",
"Apple", "X Ray", "Boat", "Tiger", "Item001", "Item002", "Item003", "Item004", "Item005",
"001Item", "002Item", "003Item", "004Item", "005Item", "Ball", "Cat", "Dog", "Fish",
"Gold Fish", "Star Fish", "2821", "2822", "2823", "2811", "2812", "2813"]
item2 = ["India", "America", "Russia", "China", "England", "Iran", "Iraq", "Ice Land"]
item3 = ["Apple","Orange","Bannana","Grapes","Jack Fruit","Black","White","Green","Blue",
"DarkBlue", "Light Blue","Red","Dark Red","Red Medium","Reddish Blue"]
fitem = None
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100, 100, 300, 30)
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(100, 150, 300, 30)
self.textbox3 = QLineEdit(self)
self.textbox3.setGeometry(100, 200, 300, 30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100, 250, 300, 500)
self.textbox1.setObjectName("textbox1")
self.textbox2.setObjectName("textbox2")
self.textbox3.setObjectName("textbox3")
self.textbox1.installEventFilter(self)
self.textbox2.installEventFilter(self)
self.textbox3.installEventFilter(self)
self.lbox2.installEventFilter(self)
# Based on focus item, I want to create some codes here.
if fitem == "textbox1":
print(" focus item is ", textbox1)
def eventFilter(self, source, event):
global fitem
if event.type() == QEvent.FocusIn:
if source == self.textbox1:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item1)
self.lbox2.addItems(item1)
fitem = "textbox1"
print("textbox 1")
elif source == self.textbox2:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item2)
self.lbox2.addItems(item2)
fitem = "textbox2"
print("textbox 2")
elif source == self.textbox3:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item3)
self.lbox2.addItems(item3)
fitem = "textbox3"
print("textbox 3")
elif source == self.lbox2:
fitem = "listbox2"
print("listbox 2")
return super(Check, self).eventFilter(source, event)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
enter code here
In your approach you have 2 problems:
Where you want to implement your logic is executed only once since it is in the constructor, and the eventFilter is executed after constructing the object.
Do not abuse the variables as they are bad practice, read Why are global variables evil?.
The logic is to create an attribute that stores the function, a function that is called when you want to implement the logic:
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100, 100, 300, 30)
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(100, 150, 300, 30)
self.textbox3 = QLineEdit(self)
self.textbox3.setGeometry(100, 200, 300, 30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100, 250, 300, 500)
self.textbox1.setObjectName("textbox1")
self.textbox2.setObjectName("textbox2")
self.textbox3.setObjectName("textbox3")
self.textbox1.installEventFilter(self)
self.textbox2.installEventFilter(self)
self.textbox3.installEventFilter(self)
self.lbox2.installEventFilter(self)
self.fitem = None
def eventFilter(self, source, event):
if event.type() == QEvent.FocusIn:
if source == self.textbox1:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item1)
self.lbox2.addItems(item1)
self.fitem = "textbox1"
elif source == self.textbox2:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item2)
self.lbox2.addItems(item2)
self.fitem = "textbox2"
elif source == self.textbox3:
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item3)
self.lbox2.addItems(item3)
self.fitem = "textbox3"
elif source == self.lbox2:
self.fitem = "listbox2"
self.foo()
return super(Check, self).eventFilter(source, event)
def foo(self):
if self.fitem == "textbox1":
print("test")
Even so I do not understand why you get too complicated, in your case it is enough to use the focusChanged
signal:
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox1 = QLineEdit()
self.textbox2 = QLineEdit()
self.textbox3 = QLineEdit()
self.lbox = QListWidget()
lay = QVBoxLayout(self)
lay.addWidget(self.textbox1)
lay.addWidget(self.textbox2)
lay.addWidget(self.textbox3)
lay.addWidget(self.lbox)
self.mapping_list = {
self.textbox1: item1,
self.textbox2: item2,
self.textbox3: item3,
}
QApplication.instance().focusChanged.connect(self.on_focusChanged)
def on_focusChanged(self, old, new):
item = self.mapping_list.get(new)
if item is not None:
self.lbox.clear()
self.lbox.addItems(item)
print(" focus item is ", new)