分享
三行代码  ›  专栏  ›  技术社区  ›  Kumar

如何在PyQt5中获取变量值,外部事件筛选器方法?

  •  0
  • Kumar  · 技术社区  · 1 周前

    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
    
    1 回复  |  直到 1 周前
        1
  •  1
  •   eyllanesc Debarati Majumder    1 周前

    你的方法有两个问题:

    逻辑是创建一个存储函数的属性,当您要实现逻辑时调用该函数:

    
    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")

    即使如此,我也不明白为什么你会变得太复杂,在你的情况下,使用 focusChanged 信号:

    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)