New Python User Trying Qt 5.1.1

Post here if you need help with creating a Graphical User Interface in Python.

New Python User Trying Qt 5.1.1

Postby Tango » Tue Oct 22, 2013 4:52 pm

Yes, I am completely new to Python. I've been using Perl and Java for years and want to start using Python.

But, at the same time, I'm working on projects in Perl and C++ (which I'm also learning) and I've installed Qt on my system, which is an iMac running OS X 10.8.4. I downloaded and built Qt 5.1.1, then installed Sip and PyQt. Then I wanted to test and be sure PyQt was working and the new Qt install overall, too. I figured it'd be quicker to test in Python than with a C++ example (and I'm having difficulties dealing with Perl - Qt on Perl is on hold).

I found this page http://pyqt.sourceforge.net/Docs/PyQt5/designer.html with some basic information on using PyQt. Other pages talk about using pyuic, but this page says it's included and that .ui files from Qt Designer will be parsed automatically by the Qt modules. So I used Qt Designer and tried to create a simple dialog - with just Cancel and Okay buttons and I saved the file, using the names specified on the linked page. Then I renamed ui_imagedialog.ui to ui-imagedialog.py (as specified in the tutorial) and created the program file by cutting and pasting this code in my text editor:

Code: Select all
#!/usr/local/bin/python3

import sys
from PyQt5.QtWidgets import QApplication, QDialog
from ui_imagedialog import Ui_ImageDialog

app = QApplication(sys.argv)
window = QDialog()
ui = Ui_ImageDialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())


(I added the 1st 2 lines.)

When I first set this up and tried it, I had not renamed ui_imagedialog.ui to ui_imagedialog.py and I got an error on the 5th line ("from ui_imagedialog import..."), which tells me that in Python, if a resource is not there, I get an error at the line where I'm trying to import. Since I didn't get any errors from the 4th line, that makes me think PyQt5 is visible and available (meaning I installed it correctly).

But once I did rename ui_imagedialog.ui to ui_imagedialog.py, I find I get this error:

Code: Select all
Traceback (most recent call last):
  File "./QtTest.py", line 5, in <module>
    from ui_imagedialog import ImageDialog
  File "/Users/hal/Documents/Dev/Puttering/ui_imagedialog.py", line 1
    <?xml version="1.0" encoding="UTF-8"?>
    ^
SyntaxError: invalid syntax


It's having trouble from the very first line in ui_imagedialog.py. According to the information on the page with this example, the .ui file is supposed to be renamed to a .py file and is supposed to be parsed by the uic module that PyQt5 uses. I figure it is being read and treated as a Python file and not being parsed as the page the code is from states.

What am I doing wrong and what do I need to change so the .ui file is parsed as it should be?
User avatar
Tango
 
Posts: 3
Joined: Tue Oct 22, 2013 4:26 pm

Re: New Python User Trying Qt 5.1.1

Postby stranac » Tue Oct 22, 2013 5:04 pm

I've never used the QT Designer, but from that site, it seems to me that you should use pyuic5 to generate the .py file from the .ui
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: New Python User Trying Qt 5.1.1

Postby Tango » Tue Oct 22, 2013 5:26 pm

That's what's confusing, since it also says:

PyQt5 does not wrap the QUiLoader class but instead includes the uic Python module. Like QUiLoader this module can load .ui files to create a user interface dynamically. Like the uic utility it can also generate the Python code that will create the user interface. PyQt5’s pyuic5 utility is a command line interface to the uic module. Both are described in detail in the following sections.
User avatar
Tango
 
Posts: 3
Joined: Tue Oct 22, 2013 4:26 pm

Re: New Python User Trying Qt 5.1.1

Postby stranac » Tue Oct 22, 2013 5:45 pm

That just means that you can load .ui files by using the uic module.
Or you can compile them to python(using pyuic5), and then import it like you're trying.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: New Python User Trying Qt 5.1.1

Postby Tango » Tue Oct 22, 2013 5:52 pm

Okay. I was reading it as it was definitely stating that the ui file would be parsed automatically and had re-read it several times and came to that conclusion.

But I figured if you were getting a different interpretation then I needed to re-examine the situation. Part of the problem is I have not been able to find pyuic5 on my system anywhere, so I wondered if it was really needed or just what was going on. I even used the find command to try to locate it.

It turns out pyuic is in the /Library directory tree - but there was a permissions issue, which very well could be my fault. Anyway, I found it using find as root, fixed the issue, and added the bin directory with pyuic5 to my path so it works now.

I've contacted them about the wording of that paragraph. You clearly understood it correctly, but it did sound to me like a new ability to parse the file directly was built in on PyQt5, so I've contacted the developers about that. I think a little bit of re-writing would make a world of difference.

Thank you!
User avatar
Tango
 
Posts: 3
Joined: Tue Oct 22, 2013 4:26 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest