如何在 QLineEdit 中显示 QListwidget 选中的项目?
How to show the QListwidget selected item in QLineEdit?
如何在 QLineEdit 中显示 QListWidget 选中的项目? .这是我的代码。在第二个 File 行中,没有 59 到 61 有问题。 问题:属性错误,None类型。如何整改?
在我的第一个文件 (example_main_file.py) 中,我有一个带有项目的 QLineEdit 和 QListWidget。我的第二个文件 (example_source_file) 有一个 方法到 process/filter 来自 QLineEdit 的 QListwidget 项目 并显示 QListwidget 选定的项目。但结果并不丰硕。
First File / example_main_file.py
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget,QListWidget
from example_source_file import Sourcefile
item = ["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"]
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox = QLineEdit(self)
self.textbox.setGeometry(100, 100, 300, 30)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100,150,300,30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100,200,300,500)
self.lbox1.addItems(item)
self.lbox2.addItems(item)
process = Sourcefile(self.textbox, self.lbox1, self.lbox2)
process1 = Sourcefile(self.textbox1, self.lbox1, self.lbox2)
self.textbox.textChanged.connect(process.func_textbox_textchanged)
self.textbox1.textChanged.connect(process1.func_textbox_textchanged)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
Second File / example_source_file.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
flag = 1
search_text_length = 0
startitem_rowno = None
enditem_rowno = None
class Sourcefile(QObject):
def __init__(self, textbox,listbox1,listbox2,parent=None):
super().__init__(listbox2)
global startitem_rowno,enditem_rowno
self.tbox1 = textbox
self.lbox1 = listbox1
self.lbox2 = listbox2
self.tbox1.installEventFilter(self)
self.lbox2.installEventFilter(self)
startitem_rowno = 0
enditem_rowno = len(self.lbox2) - 1
def eventFilter(self, source, event):
global cursor_position, textbox_value
if event.type() == QEvent.KeyPress and source is self.tbox1:
if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_S:
self.func_item_startswith()
return True
if event.key() == Qt.Key_Down:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(startitem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Up:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(enditem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Return:
if len(self.lbox2) == 1:
self.lbox2.setCurrentRow(0)
prin = self.lbox2.currentItem().text()
print(prin)
if event.type() == QEvent.KeyPress and source is self.lbox2:
if event.key() == Qt.Key_Left or event.key() == Qt.Key_Backspace:
self.lbox2.clearFocus()
self.tbox1.setFocus()
elif event.key() == Qt.Key_Return:
prin = self.lbox2.currentItem().text()
print(prin)
self.tbox1.setText(prin)
#self.label_selected_item.setText("Selected Item : " + prin)
#self.label_selected_item.adjustSize()
self.tbox1.setFocus()
self.tbox1.setText(prin)
elif event.key() == Qt.Key_Up or event.key() == Qt.Key_Down:
pass
return super(Sourcefile, self).eventFilter(source, event)
def func_item_startswith(self):
global flag, startitem_rowno, enditem_rowno
flag = 1
startitem_rowno = 0
enditem_rowno = startitem_count - 1
self.lbox2.clear()
if startitem_count > 0:
for item in item_startswith:
self.lbox2.addItem(item.text())
else:
print("No Matching from start item")
def func_item_normal(self):
global falg,startitem_rowno,enditem_rowno
flag = 0
startitem_rowno = 0
enditem_rowno = normal_count - 1
self.lbox2.clear()
if normal_count > 0:
for item in item_normal:
self.lbox2.addItem(item.text())
def func_textbox_textchanged(self, txt):
global search_text, search_text_length, total_listbox_item, availableitem_count, normal_count, startitem_count, \
containitem_count, enditem_count, item_normal, item_startswith, item_contains, item_endswith, flag, \
startitem_rowno, enditem_rowno
search_text = self.tbox1.text()
search_text_length = len(search_text)
total_listbox_item = len(self.lbox2)
item_normal = self.lbox1.findItems("*", Qt.MatchWildcard)
item_startswith = self.lbox1.findItems(search_text, Qt.MatchStartsWith)
normal_count = len(item_normal)
startitem_count = len(item_startswith)
self.func_item_normal()
if search_text_length >= 1: pass
else: flag = 1
self.lbox2.clear()
if flag == 1:
self.func_item_startswith()
else:
self.func_item_normal()
def listbox_clicked(self, item):
self.tbox1.setText(item.text())
self.tbox1.setFocus()
Error / Result
Traceback (most recent call last):
File "D:\PyQt5_samples\example_source_file.py", line 59, in eventFilter
prin = self.lbox2.currentItem().text()
AttributeError: 'NoneType' object has no attribute 'text'
解释:
当您按下 enter 键时,项目被选中,它被复制到 QLineEdit 的文本中,然后调用项目过滤器删除该项目,因此下一个项目中不再有 currentItem,因此它 returns 错误。
解决方案:
鉴于此,有 2 种可能的解决方案:
- 通过返回 True 防止事件继续触发。
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
self.tbox1.setText(item.text())
self.tbox1.setFocus()
<b>return True</b>
- 验证该项目不是 None。
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
<b>if item is not None:</b>
self.tbox1.setText(item.text())
self.tbox1.setFocus()
如何在 QLineEdit 中显示 QListWidget 选中的项目? .这是我的代码。在第二个 File 行中,没有 59 到 61 有问题。 问题:属性错误,None类型。如何整改?
在我的第一个文件 (example_main_file.py) 中,我有一个带有项目的 QLineEdit 和 QListWidget。我的第二个文件 (example_source_file) 有一个 方法到 process/filter 来自 QLineEdit 的 QListwidget 项目 并显示 QListwidget 选定的项目。但结果并不丰硕。
First File / example_main_file.py
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget,QListWidget
from example_source_file import Sourcefile
item = ["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"]
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox = QLineEdit(self)
self.textbox.setGeometry(100, 100, 300, 30)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100,150,300,30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100,200,300,500)
self.lbox1.addItems(item)
self.lbox2.addItems(item)
process = Sourcefile(self.textbox, self.lbox1, self.lbox2)
process1 = Sourcefile(self.textbox1, self.lbox1, self.lbox2)
self.textbox.textChanged.connect(process.func_textbox_textchanged)
self.textbox1.textChanged.connect(process1.func_textbox_textchanged)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
Second File / example_source_file.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
flag = 1
search_text_length = 0
startitem_rowno = None
enditem_rowno = None
class Sourcefile(QObject):
def __init__(self, textbox,listbox1,listbox2,parent=None):
super().__init__(listbox2)
global startitem_rowno,enditem_rowno
self.tbox1 = textbox
self.lbox1 = listbox1
self.lbox2 = listbox2
self.tbox1.installEventFilter(self)
self.lbox2.installEventFilter(self)
startitem_rowno = 0
enditem_rowno = len(self.lbox2) - 1
def eventFilter(self, source, event):
global cursor_position, textbox_value
if event.type() == QEvent.KeyPress and source is self.tbox1:
if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_S:
self.func_item_startswith()
return True
if event.key() == Qt.Key_Down:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(startitem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Up:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(enditem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Return:
if len(self.lbox2) == 1:
self.lbox2.setCurrentRow(0)
prin = self.lbox2.currentItem().text()
print(prin)
if event.type() == QEvent.KeyPress and source is self.lbox2:
if event.key() == Qt.Key_Left or event.key() == Qt.Key_Backspace:
self.lbox2.clearFocus()
self.tbox1.setFocus()
elif event.key() == Qt.Key_Return:
prin = self.lbox2.currentItem().text()
print(prin)
self.tbox1.setText(prin)
#self.label_selected_item.setText("Selected Item : " + prin)
#self.label_selected_item.adjustSize()
self.tbox1.setFocus()
self.tbox1.setText(prin)
elif event.key() == Qt.Key_Up or event.key() == Qt.Key_Down:
pass
return super(Sourcefile, self).eventFilter(source, event)
def func_item_startswith(self):
global flag, startitem_rowno, enditem_rowno
flag = 1
startitem_rowno = 0
enditem_rowno = startitem_count - 1
self.lbox2.clear()
if startitem_count > 0:
for item in item_startswith:
self.lbox2.addItem(item.text())
else:
print("No Matching from start item")
def func_item_normal(self):
global falg,startitem_rowno,enditem_rowno
flag = 0
startitem_rowno = 0
enditem_rowno = normal_count - 1
self.lbox2.clear()
if normal_count > 0:
for item in item_normal:
self.lbox2.addItem(item.text())
def func_textbox_textchanged(self, txt):
global search_text, search_text_length, total_listbox_item, availableitem_count, normal_count, startitem_count, \
containitem_count, enditem_count, item_normal, item_startswith, item_contains, item_endswith, flag, \
startitem_rowno, enditem_rowno
search_text = self.tbox1.text()
search_text_length = len(search_text)
total_listbox_item = len(self.lbox2)
item_normal = self.lbox1.findItems("*", Qt.MatchWildcard)
item_startswith = self.lbox1.findItems(search_text, Qt.MatchStartsWith)
normal_count = len(item_normal)
startitem_count = len(item_startswith)
self.func_item_normal()
if search_text_length >= 1: pass
else: flag = 1
self.lbox2.clear()
if flag == 1:
self.func_item_startswith()
else:
self.func_item_normal()
def listbox_clicked(self, item):
self.tbox1.setText(item.text())
self.tbox1.setFocus()
Error / Result
Traceback (most recent call last):
File "D:\PyQt5_samples\example_source_file.py", line 59, in eventFilter
prin = self.lbox2.currentItem().text()
AttributeError: 'NoneType' object has no attribute 'text'
解释:
当您按下 enter 键时,项目被选中,它被复制到 QLineEdit 的文本中,然后调用项目过滤器删除该项目,因此下一个项目中不再有 currentItem,因此它 returns 错误。
解决方案:
鉴于此,有 2 种可能的解决方案:
- 通过返回 True 防止事件继续触发。
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
self.tbox1.setText(item.text())
self.tbox1.setFocus()
<b>return True</b>
- 验证该项目不是 None。
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
<b>if item is not None:</b>
self.tbox1.setText(item.text())
self.tbox1.setFocus()