Well, I think you could do one of two realistic things:
- Compile your .ui files into .py files using pyside-uic and modify your code to conditionally load the py files for the user interface and put the png files in the Qt resource file.
- Create a Qt Resource file with your user files inside it, compile it with pyside-rcc, and then load the user files using QtUiTools or some similar process.
pyside-uic
I really prefer using the pyside-uic method to load user interface files, because this is the easiest way to load user interface files into a program that matches my knowledge of Qt in C ++. pyside-uic included in PySide applications, and for me it is in the Scripts directory of my Python installation, for example, C:\Python27\Scripts\pyside-uic.exe . Given the way C ++ compilation processes user interface files, I usually compile my user files to have a name like ui_ [User Interface File Name] .py:
C:\Python27\Scripts\pyside-uic mainwindow.ui > ui_mainwindow.py
Inside this resulting .py file, pyside-uic creates a class with the same name as the base class of the ui file, with the addition of Ui_. So, for example, if you created mainwindow.ui that contains a definition for a class named MainWindow, the created class will be Ui_MainWindow. If the user interface file defines a class named SourceWindow, the class in the .py file is Ui_SourceWindow. In Qt Designer, you set the class name by setting objectName in the root element of the object tree (in the upper right corner of the window).
With your cafeteria_menu, ui, and dialog_login.ui files, you get the derived classes Ui_cafeteria_menu and Ui_dialog_login.
Once you have created the .py file, you can use it by importing it into the definition file for your widget and using the setupUi method of the class in the Ui file.
from PySide import QtCore, QtGui from ui_mainwindow import Ui_MainWindow class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self)
After you have defined the user interface for the class, all connections and user interface elements for the widget should be accessible through self.ui
self.ui.lineEdit.textChanged[str].connect(self.processText)
Since you will need to put your .png files in a Qt resource file, I will talk about this in the next section.
pyside-rcc
Like pyside-uic , pyside-rcc included in the PySide application, although mine is located in the site-packages directory in Python and not in Scripts (if it is there for you, you can always copy it).
C:\Python27\lib\site-packages\PySide\pyside-rcc.exe
Before you can compile a Qt resource file, you must first create it using one of the Qt tools. I use Qt Creator, since it can perform almost all the functions related to Qt in one application. The documentation for the Qt Resource System shows that the resource file is actually just an XML file that defines file paths and internal paths for the resource system. You can install and organize files as you wish, but when it comes to compilation, all files defined in the resource file must be in the same directory or subdirectory of the file. After you have defined the resource file, you need to use pyside-rcc.exe to compile it into a .py file. I usually call the resource file the same as the project, and store everything in one resource file to make working with resources more concise.
C:\Python27\lib\site-packages\PySide\pyside-rcc.exe -py2 MyProject.qrc > MyProject_Resources.py
The -py2 switch specifies that the output from the file should be formatted for Python 2.x. If you use Python 3.x or plan to use it in the future, you can use the -py3 switch and the result will be compatible with Python 3.x.
Putting it all together
Since you are already loading the QUiLoader user QUiLoader directly, QUiLoader , you just need to reorganize your QUiLoader statements to load the QFile, which opens the UI resource from the resource system. To use files from the resource system, all you have to do is import your Resource.py file generated from pyside-rcc into the main script file of your program, and the last line in the resource file is the qInitResources () call, which initializes resources to be used throughout the program. To upload a file using QFile, use a path that starts with ":" and then refers to the paths that were defined in the resource file. You can create the msResources.qrc file with a user interface and images in which your user files and png files are defined as subcategories.
So, if your resource file looks something like this
/ui cafeteria_menu.ui dialog_login.ui /images cafeteria-menu.png exit.png logo.png mail-fetch.ong
And, if you want to download these files, you just need to create a QIcon or QFile something like this:
cafeteriaMenuIcon = QtGui.QIcon(":/images/cafeteria-menu.png") cafeteriaMenuUi = QtCore.QFile(":/ui/cafeteria_menu.ui")
When using GUICafeteriaMenu in msCafeteriaMenu in your code, I would just change the __init__ method for GuiCafeteriaMenu to load and use the user interface file from resources:
uiFile = QFile(":/ui/cafeteria_menu.ui") uiFile.open(QFile.ReadOnly) UiLoader.load(uiFile, self) uiFile.close()
I would probably put the output of pyside-rcc in the metsuite_libs package into something like msResources.py and import the msResources file into the __init__.py file as part of your package. That way, once you have the .py files created and imported into your program, the extra file will be encapsulated in your package and you will not need to modify the setup.py file. Before executing the py2exe conversion, starting the processed program should work fine. In addition, no matter how you process the user interface files, you will always need to use a resource file in order to be able to pack icons into a program. For portability reasons, using resource files for icons is probably a good habit.