Can't add item to QLIstWidget
Trying to add an item to a QListWidget but it won't add the item to the list.
code:
dirListLayout = QListWidget()
def dropped(dir):
dirListLayout.addItem(dir)
print("dropped and added")
dir is a string "test" in this case.
It doesn't show any error. it does print "dropped and added"
Thanks in advance.
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
AttributeError: 'QAction' object has no attribute 'setShortcutVisibleInContextMenu'
I am using Spyder on Windows.
These are my python version and modules:
PS C:\Windows\system32> python --version Python 3.9.10 PS C:\Windows\system32> pip list Package Version ------------------ --------- imageio 2.18.0 joblib 1.1.0 networkx 2.8 numpy 1.22.3 packaging 21.3 Pillow 9.1.0 pip 22.0.4 pyparsing 3.0.8 PyQt5 5.15.6 PyQt5-Qt5 5.15.2 PyQt5-sip 12.10.1 PySide6 6.3.0 PySide6-Addons 6.3.0 PySide6-Essentials 6.3.0 PyWavelets 1.3.0 scikit-image 0.19.2 scikit-learn 1.0.2 scipy 1.8.0 setuptools 58.1.0 shiboken6 6.3.0 threadpoolctl 3.1.0 tifffile 2022.4.28
This is my code related to this error:
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget,QInputDialog,QFileDialog from PyQt5.QtGui import QIcon, QPixmap from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * self.actionOpen_Source.setShortcutVisibleInContextMenu(True)
This is the error message:
File "T:\OOP-2 Lab Final\oop2labfinal.py", line 393, in setupUi self.actionOpen_Source.setShortcutVisibleInContextMenu(True) AttributeError: 'QAction' object has no attribute 'setShortcutVisibleInContextMenu'
-
PyQT5 doesn't work on docker ImportError: libsmime3.so: cannot open shared object file: No such file or directory
I have a Dockerfile with PyQT installed like below
FROM ubuntu:20.04 ENV DEBIAN_FRONTEND=noninteractive RUN adduser --quiet --disabled-password qtuser && usermod -a -G audio qtuser RUN apt-get update -y \ && apt-get install alsa -y \ && apt-get install -y python3-pyqt5 \ && apt-get install python3-pip -y && \ pip3 install pyqtwebengine WORKDIR /htmltopdf
I built my image like this
docker build -t html-to-pdf .
Then I ran my image like this
docker run --rm -v "$(pwd)":/htmltopdf -u qtuser -it html-to-pdf python3 htmlToPdfnew.py --url https://www.w3schools.com/howto/howto_css_register_form.asp
But I'm getting below error
Traceback (most recent call last): File "htmlToPdfnew.py", line 2, in <module> from PyQt5 import QtWidgets, QtWebEngineWidgets ImportError: libsmime3.so: cannot open shared object file: No such file or directory
I do NOT get that error in my PC.
below is my python code
import sys from PyQt5 import QtWidgets, QtWebEngineWidgets from PyQt5.QtCore import QUrl, QTimer from PyQt5.QtGui import QPageLayout, QPageSize from PyQt5.QtWidgets import QApplication import argparse def main(): url = '' parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--url", help="Type url") args = parser.parse_args() config = vars(args) url = config['url'] app = QtWidgets.QApplication(sys.argv) loader = QtWebEngineWidgets.QWebEngineView() loader.setZoomFactor(1) layout = QPageLayout() layout.setPageSize(QPageSize(QPageSize.A4Extra)) layout.setOrientation(QPageLayout.Portrait) loader.load(QUrl(url)) loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit()) def emit_pdf(finished): QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout)) loader.loadFinished.connect(emit_pdf) sys.exit(app.exec_()) if __name__ == '__main__': main()
so how do I resolve below error ?
Traceback (most recent call last): File "htmlToPdfnew.py", line 2, in <module> from PyQt5 import QtWidgets, QtWebEngineWidgets ImportError: libsmime3.so: cannot open shared object file: No such file or directory
-
to import pyqt5.uic, do I have to install pyqt5-tools first?
to import pyqt5.uic, do I have to install pyqt5-tools first?
(venv) PS C:\Users\77140\AppData\Roaming\Anki2\addons21\myaddon> pip freeze | grep PyQt PyQt5-Qt5==5.15.2 PyQt5-sip==12.10.1 PyQt5-stubs==5.15.6.0 PyQtWebEngine==5.15.5 PyQtWebEngine-Qt5==5.15.2
But My python version is 3.10, from pyqt5-tools offical site, it doesn't support python 3.10, is it why when I exec
pip install pyqt5-tools
, always get the below error?:Thanks a lot.
(venv) PS C:\Users\77140\AppData\Roaming\Anki2\addons21\myaddon> pip install pyqt5-tools Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting pyqt5-tools Using cached https://pypi.tuna.tsinghua.edu.cn/packages/b7/70/a25aab849a5e38ae78716acde1f917a14d9d819c8600bbb8cf536781f7c5/pyqt5_tools-5.15.4.3.2-py3-none-any.whl (29 kB) Requirement already satisfied: click in c:\users\77140\appdata\roaming\anki2\addons21\myaddon\venv\lib\site-packages (from pyqt5-tools) (8.1.3) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/36/b8/a255b8160e863678aa7d559a4c8c33b5448a6e270992d1464ec9f87ed633/pyqt5_tools-5.15.4.3.1-py3-none-any.whl (28 kB) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/88/ca/a9cb4816479940d1e0c94c683418c2cc4b4bc2955cd3c5754179a5f95040/pyqt5_tools-5.15.4.3.0.3-py3-none-any.whl (28 kB) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/e5/cb/34b583ce53ca66803cfe904b11ae87633b1e98121a2a54583674514ae29b/pyqt5_tools-5.15.3.3.2-py3-none-any.whl (29 kB) Collecting pyqt5==5.15.3 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/6e/86/d715e71771cece0e060f2ebab20f3ded067b08a0927dfb3143530cae8098/PyQt5-5.15.3.tar.gz (3.3 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [29 lines of output] Traceback (most recent call last): File "c:\users\77140\appdata\roaming\anki2\addons21\myaddon\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 156, in prepare_metadata_for_build_wheel hook = backend.prepare_metadata_for_build_wheel AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\users\77140\appdata\roaming\anki2\addons21\myaddon\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 363, in <module> main() File "c:\users\77140\appdata\roaming\anki2\addons21\myaddon\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 345, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "c:\users\77140\appdata\roaming\anki2\addons21\myaddon\venv\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py", line 160, in prepare_metadata_for_build_wheel whl_basename = backend.build_wheel(metadata_directory, config_settings) File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\sipbuild\api.py", line 51, in build_wheel project = AbstractProject.bootstrap('pep517') File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\sipbuild\abstract_project.py", line 83, in bootstrap project.setup(pyproject, tool, tool_description) File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\sipbuild\project.py", line 594, in setup self.apply_user_defaults(tool) File "C:\Users\77140\AppData\Local\Temp\pip-install-8c05wpmz\pyqt5_4b97949f3a094e468dd52d10bb2e71c4\project.py", line 63, in apply_user_defaults super().apply_user_defaults(tool) File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\pyqtbuild\project.py", line 70, in apply_user_defaults super().apply_user_defaults(tool) File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\sipbuild\project.py", line 241, in apply_user_defaults self.builder.apply_user_defaults(tool) File "C:\Users\77140\AppData\Local\Temp\pip-build-env-fnfit907\overlay\Lib\site-packages\pyqtbuild\builder.py", line 67, in apply_user_defaults sipbuild.pyproject.PyProjectOptionException sipbuild.pyproject.PyProjectOptionException [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.