Solved libQt5Core5 missmatch (beginner)
-
from PyQt5.QtWidgets import QApplication, uic
Where did you get idea of
from PyQt5/PySide2.QtWidgets import uic
from?For PySide2:
Do you want to read and follow approach from https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html#loading-it-directly ? Essentially
from PySide2.QtUiTools import QUiLoader ui_file = QFile("mainwindow.ui") ui_file.open(QFile.ReadOnly) loader = QUiLoader() window = loader.load(ui_file) window.show()
For PyQt5:
It's
from PyQt5 import uic
, notQtWidgets
. See https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html & https://www.riverbankcomputing.com/static/Docs/PyQt5/api/uic/uic-module.htmlThat's to let you load the
.ui
file dynamically at run-time. If you want to generate a Python class at design-time, there is (command-line tools)pyside2-uic
orpyuic5
for PyQt. Or similar, search for these.If you want advice on which approach to take, I would say the design-time generation of classes probably gives you easier access to the UI objects created. You have to get used to running the
pyuic
bit whenever you change the UI in the designer.You probably want to make your decision about whether you want to go PySide2 or PyQt5, and stick with it. I don't know what your criteria are.
-
With PyQt5, first you use the
pyuic5
tool that will have been installed at the same time as PyQt5 (assuming you usedpip
) to convert your.ui
file into a python module:$ pyuic5 -x mainw.ui -o mainw.py
You then import this module into your main application code and use it thus:
from PyQt5.QtWidgets import QMainWindow from mainw import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.ui=UI_MainWindow() self.ui.setupUI(self) if __name__=='__main__': from sys import argv, exit from PyQt5.QtWidgets import QApplication a=QApplication(argv) m=MainWindow() m.show() exit(a.exec_())
Hope this helps :o)
-
"With PyQt5, first you use the pyuic5 tool that will have been installed at the same time as PyQt5...."
@jazzycamel I believe that is one of possible methods (?). I read through tutorials, posts and books and I'd swear I read explicitely, that those are two different ways to use UI file. Also I'm following tutorials for this method.
I know I can convert, but I deliberately want to use UI files at this stage, because I'd prefer to use them at design stage. When the particular forms are stable, I might get into habit to export them, if it will help performance noticeably.
I'm not saying I won't end up using this method as standard later, I'm only learning at this stage.I'll try the conversion as per your code and instructionss as another training, it won't hurt me :-).
EDIT:
I get command-not-found when trying to run:pyuic5 -x mainw.ui -o mainw.py
So I'm starting to think those two issues are connected.
Again, I do have PyQt5 installed:
> pip install --upgrade PyQt5 Defaulting to user installation because normal site-packages is not writeable Requirement already up-to-date: PyQt5 in /home/<user>/.local/lib/python3.6/site-packages (5.14.1) Requirement already satisfied, skipping upgrade: PyQt5-sip<13,>=12.7 in /home/<user>/.local/lib/python3.6/site-packages (from PyQt5) (12.7.1)
...but the strange "Defaulting to user installation" message might be a clue. My python is in /usr/bin/Python3, my libraries in /usr/lib64/python/ (symlink), but here it looks into user directory /home/<user>/.local/lib/python3.6/site-packages. When I run following command:
> python -m site sys.path = [ '/home/libor/Coding/Projects/Test2', '/usr/lib/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/local/lib64/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/local/lib64/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages', ] USER_BASE: '/home/<user>/.local' (exists) USER_SITE: '/home/<user>/.local/lib/python2.7/site-packages' (doesn't exist) ENABLE_USER_SITE: True
...I think I see the evil: USER_SITE points to different site-packages, from Python 2.7.
So can I simply do following??
export USER_SITE=/home/<user>/.local/lib/python3.6/site-packages
Does it look like a solution?
-
@Oak77 as a brief summary, there are 3 methods to create your GUIs:
- Hand-coded GUI
- Tool-coded GUI from .ui file (using pyside2-uic or pyqt-uic)
- Loading of .ui file at runtime
-
@Oak77 said in libQt5Core5 missmatch (beginner):
python -m site
Before you go any further! Under Linux
python
executes Python 2. That's why you're seeing all thosepython2.7
directories. You do not want this at all! Everything you want to do (for Qt) you need to usepython3
as the command interpreter, for Python 3!! -
@JonB You're spot on, but I didn't think it was a problem. Running
> python Python 2.7.17 (default, Jan 24 2020, 15:33:58) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
...as oposed to:
> python3 Python 3.6.10 (default, Jan 16 2020, 09:12:04) [GCC] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
So in terminal, it seems to really run pyuic from 2.7, not 3.6.
By this time I am totally confused how to make Pythons consistent while maintaining existing dependencies in the OS.
QtCreator is set to 3.6.10 (Tools --> Options: Python: Interpreters: Make default 3.6.10) as mentioned at the beginnig and the project to run in the same environment (Interpreter = Python 3.6.10).
What else I can do to get things working?
I asume that Python 2.7.17 is sitting there due to dependencies of intalled applications.
-
@Oak77
If you can't find thepyuic5
command itself, but have PyQt5 installed and importable, then runpyuic
as a module instead:$ python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py
For future reference, I always recomend using a seperate
virtualenv
for each python project you develop. That way you always know where the libraries/tools are and you don't get cross-contamination or version issues.Hope this helps :o)
-
"python3 -m PyQt5.uic.pyuic -x mainw.ui -o mainw.py"
@jazzycamel - That worked and once I see it, I understand why. Thank you very much.
I tried one project and set it with virtual environment, but it downloaded some 320 GB of data (for only some dozens of bytes long tutorial). So it can spiral out into significant gigabytes for only few kilobytes I program at the moment, while learning. It seems to me a bit unreasonagle, unless for projects with specific needs. Unless I misunderstood something, which I easily could, at this stage.
And I also have to get to undertand how Python is set up anyway, if I want to use it, so I'll am testing both ways. -
@Oak77
I'm not sure why it would download that much data for a virtualenv, I must have 30 or 40+ on my machine, they are basically throw away tiny things.Anyways, I'm glad it worked for you :o)
-
To close this thread, this is final working code:
import sys from PySide2 import QtWidgets from PySide2.QtUiTools import QUiLoader from PySide2.QtWidgets import QApplication class Form(QObject): def __init__(self, ui_file, parent=None): super(Form, self).__init__(parent) ui_file = QFile(ui_file) ui_file.open(QFile.ReadOnly) self.window.show() if __name__ == '__main__': app = QApplication(sys.argv) form = Form('mainwindow.ui') sys.exit(app.exec_())