How to write text to listWidget?
-
I realize this is not the forum to ask basic Python questions, but I have no access to another Python forum.
I have a simple code to clear listWidget , but I cannot figure out the correct syntax to add simple text to listWidget.
Here is my code
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.clear) item_TEST = " Initialize debug TRACE" # self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.setToolTip(" Initialize debug TRACE ")) self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) ) and its resulting error :
File "/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/NANO_WORK/src/NanoVNASaver/NanoVNASaver.py", line 348, in init
"DISPLAY_FORM": TDRWindow_COPY(self), # copy of TDRWindow
File "/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/NANO_WORK/src/NanoVNASaver/Windows/TRACE_FORM.py", line 272, in init
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) )
AttributeError: 'TDRWindow_COPY' object has no attribute 'item_TEST'In simple terms I can "clear " the listWidget, but I cannot add SIMPLE text to it. MrsGoogle showed me how to add items from a list, but I want to add SINGLE line of text.
The above is a copy from word processor so it may not meet code formatting.
If that is a problem , please IGNORE the post - there is no need to inform me about "code formatting" - it does not solve my code problem.It would be nice if somebody just wrote correct code and give some explanation why my syntax is wrong .
wrong syntax:
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) )Correct syntax here
.....
your syntax IS WRONG BECAUSE HERE....
Thanks
-
I realize this is not the forum to ask basic Python questions, but I have no access to another Python forum.
I have a simple code to clear listWidget , but I cannot figure out the correct syntax to add simple text to listWidget.
Here is my code
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.clear) item_TEST = " Initialize debug TRACE" # self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.setToolTip(" Initialize debug TRACE ")) self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) ) and its resulting error :
File "/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/NANO_WORK/src/NanoVNASaver/NanoVNASaver.py", line 348, in init
"DISPLAY_FORM": TDRWindow_COPY(self), # copy of TDRWindow
File "/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/NANO_WORK/src/NanoVNASaver/Windows/TRACE_FORM.py", line 272, in init
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) )
AttributeError: 'TDRWindow_COPY' object has no attribute 'item_TEST'In simple terms I can "clear " the listWidget, but I cannot add SIMPLE text to it. MrsGoogle showed me how to add items from a list, but I want to add SINGLE line of text.
The above is a copy from word processor so it may not meet code formatting.
If that is a problem , please IGNORE the post - there is no need to inform me about "code formatting" - it does not solve my code problem.It would be nice if somebody just wrote correct code and give some explanation why my syntax is wrong .
wrong syntax:
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) )Correct syntax here
.....
your syntax IS WRONG BECAUSE HERE....
Thanks
@AnneRanch said in How to write text to listWidget?:
self.pushButton_InitializeTRACE.clicked.connect(self.listWidget.addItem(self.item_TEST) )
Try:
self.pushButton_InitializeTRACE.clicked.connect( lambda: self.listWidget.addItem(self.item_TEST) )
You need a
lambda
here for the slot, that's how you specify inline code like you have to execute rather than connecting to a method.