[PyQt] Confusion about load.uic() and import regarding pyinstaller

Barry barry at barrys-emacs.org
Thu Mar 7 21:02:09 GMT 2019


Sounds like you need the pyinstaller people to help you.

Barry

> On 7 Mar 2019, at 18:42, Hans Jörg Maurer <hjm at pmeonline.net> wrote:
> 
> Hi there,
> 
> I tried to create an exe file on Windows 10  with pyinstaller 0.9.2.2. python 3.7, pyQt 5.12 And ended up in a problem with the load.uic() / import from uifile...
> Finally I got it working so far. But there are some problems upcoming.
> 1. To use the load.uic() command the ui file must be placed in the directory of the exe file. I want to have a single file, so I used
> 2. "pyuic5 testmainwindow.ui -o testmainwindow_ui.py" and "from testmainwindow_ui import Ui_MainWindow" 
> 
> Both did run, but if I use the import and set e.g. "self.ui.setWindowTitle('Do not read my title')" the app crashes with "AttributeError: 'Ui_MainWindow' object has no attribute 'setWindowTitle'" I don't understand the reason and in addition I have no idea how to solve this. Further ith hangs on 
>  widgetList = self.ui.findChildren (QPushButton)
> AttributeError: 'Ui_MainWindow' object has no attribute 'findChildren'
> The idea behind was that I compile the ui before I run pyinstaller to avoid problems at runtime.
> 
> 
> To test the behavior just set useUIfile to True in the following example:
> My question: Where is the fault and a how to solve?
> 
> import sys
> from PyQt5 import uic
> from PyQt5.QtWidgets import QMainWindow, QApplication
> 
> class window(QMainWindow):
>     def __init__(self):
>         super().__init__()
>         self.init_UI()
> 
>     def init_UI(self):
>         useUIfile = True       # Change to false to load by raw file
> 
>         if useUIfile:
>             # importing file created with  pyuic5 testmainwindow.ui -o testmainwindow_ui.py
>             from testmainwindow_ui import Ui_MainWindow # not nice here, but also not wrong
>             self.ui = Ui_MainWindow()
>             self.ui.setupUi(self)
>         else:
>             # importing "raw" QtCreator file
>             self.ui = uic.loadUi("testmainwindow.ui" ,self)
> 
>         # One of the failings
>         self.ui.setWindowTitle("My Window is clean")
> 
> if __name__ == "__main__":  # Die Hauptfunktion der Klasse
>     app = QApplication(sys.argv)
>     prog = window()
>     prog.show()
>     sys.exit(app.exec())
> 
> 
> The testmainwindow.ui:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <ui version="4.0">
>  <class>MainWindow</class>
>  <widget class="QMainWindow" name="MainWindow">
>   <property name="geometry">
>    <rect>
>     <x>0</x>
>     <y>0</y>
>     <width>440</width>
>     <height>310</height>
>    </rect>
>   </property>
>   <property name="windowTitle">
>    <string>MainWindow</string>
>   </property>
>   <widget class="QWidget" name="centralwidget">
>    <widget class="QLabel" name="label_keyName">
>     <property name="geometry">
>      <rect>
>       <x>110</x>
>       <y>80</y>
>       <width>43</width>
>       <height>13</height>
>      </rect>
>     </property>
>     <property name="text">
>      <string>Key:</string>
>     </property>
>    </widget>
>    <widget class="QLabel" name="label">
>     <property name="geometry">
>      <rect>
>       <x>50</x>
>       <y>20</y>
>       <width>43</width>
>       <height>13</height>
>      </rect>
>     </property>
>     <property name="text">
>      <string>Source:</string>
>     </property>
>    </widget>
>    <widget class="QPushButton" name="pushButton_chooseFile">
>     <property name="geometry">
>      <rect>
>       <x>350</x>
>       <y>20</y>
>       <width>80</width>
>       <height>20</height>
>      </rect>
>     </property>
>     <property name="text">
>      <string>PushButton</string>
>     </property>
>    </widget>
>    <widget class="QLabel" name="label_2">
>     <property name="geometry">
>      <rect>
>       <x>50</x>
>       <y>50</y>
>       <width>43</width>
>       <height>13</height>
>      </rect>
>     </property>
>     <property name="text">
>      <string>Sheets:</string>
>     </property>
>    </widget>
>    <widget class="QDialogButtonBox" name="buttonBox_exit">
>     <property name="geometry">
>      <rect>
>       <x>70</x>
>       <y>240</y>
>       <width>341</width>
>       <height>32</height>
>      </rect>
>     </property>
>     <property name="orientation">
>      <enum>Qt::Horizontal</enum>
>     </property>
>     <property name="standardButtons">
>      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
>     </property>
>    </widget>
>    <widget class="QListWidget" name="listWidget_headers">
>     <property name="geometry">
>      <rect>
>       <x>250</x>
>       <y>100</y>
>       <width>151</width>
>       <height>141</height>
>      </rect>
>     </property>
>    </widget>
>    <widget class="QLabel" name="label_Headers">
>     <property name="geometry">
>      <rect>
>       <x>250</x>
>       <y>80</y>
>       <width>43</width>
>       <height>13</height>
>      </rect>
>     </property>
>     <property name="text">
>      <string>Fields:</string>
>     </property>
>    </widget>
>    <widget class="QLineEdit" name="lineEdit_fileName">
>     <property name="geometry">
>      <rect>
>       <x>110</x>
>       <y>20</y>
>       <width>231</width>
>       <height>20</height>
>      </rect>
>     </property>
>    </widget>
>    <widget class="QListWidget" name="listWidget_keys">
>     <property name="geometry">
>      <rect>
>       <x>110</x>
>       <y>100</y>
>       <width>131</width>
>       <height>141</height>
>      </rect>
>     </property>
>    </widget>
>    <widget class="QComboBox" name="comboBox_chooseSheet">
>     <property name="geometry">
>      <rect>
>       <x>110</x>
>       <y>50</y>
>       <width>231</width>
>       <height>22</height>
>      </rect>
>     </property>
>    </widget>
>   </widget>
>   <widget class="QMenuBar" name="menubar">
>    <property name="geometry">
>     <rect>
>      <x>0</x>
>      <y>0</y>
>      <width>440</width>
>      <height>19</height>
>     </rect>
>    </property>
>   </widget>
>   <widget class="QStatusBar" name="statusbar"/>
>  </widget>
>  <resources/>
>  <connections/>
> </ui>
> 
> And the testmainwindow_ui.py:
> 
> # -*- coding: utf-8 -*-
> 
> # Form implementation generated from reading ui file 'testmainwindow.ui'
> #
> # Created by: PyQt5 UI code generator 5.12
> #
> # WARNING! All changes made in this file will be lost!
> 
> from PyQt5 import QtCore, QtGui, QtWidgets
> 
> 
> class Ui_MainWindow(object):
>     def setupUi(self, MainWindow):
>         MainWindow.setObjectName("MainWindow")
>         MainWindow.resize(440, 310)
>         self.centralwidget = QtWidgets.QWidget(MainWindow)
>         self.centralwidget.setObjectName("centralwidget")
>         self.label_keyName = QtWidgets.QLabel(self.centralwidget)
>         self.label_keyName.setGeometry(QtCore.QRect(110, 80, 43, 13))
>         self.label_keyName.setObjectName("label_keyName")
>         self.label = QtWidgets.QLabel(self.centralwidget)
>         self.label.setGeometry(QtCore.QRect(50, 20, 43, 13))
>         self.label.setObjectName("label")
>         self.pushButton_chooseFile = QtWidgets.QPushButton(self.centralwidget)
>         self.pushButton_chooseFile.setGeometry(QtCore.QRect(350, 20, 80, 20))
>         self.pushButton_chooseFile.setObjectName("pushButton_chooseFile")
>         self.label_2 = QtWidgets.QLabel(self.centralwidget)
>         self.label_2.setGeometry(QtCore.QRect(50, 50, 43, 13))
>         self.label_2.setObjectName("label_2")
>         self.buttonBox_exit = QtWidgets.QDialogButtonBox(self.centralwidget)
>         self.buttonBox_exit.setGeometry(QtCore.QRect(70, 240, 341, 32))
>         self.buttonBox_exit.setOrientation(QtCore.Qt.Horizontal)
>         self.buttonBox_exit.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
>         self.buttonBox_exit.setObjectName("buttonBox_exit")
>         self.listWidget_headers = QtWidgets.QListWidget(self.centralwidget)
>         self.listWidget_headers.setGeometry(QtCore.QRect(250, 100, 151, 141))
>         self.listWidget_headers.setObjectName("listWidget_headers")
>         self.label_Headers = QtWidgets.QLabel(self.centralwidget)
>         self.label_Headers.setGeometry(QtCore.QRect(250, 80, 43, 13))
>         self.label_Headers.setObjectName("label_Headers")
>         self.lineEdit_fileName = QtWidgets.QLineEdit(self.centralwidget)
>         self.lineEdit_fileName.setGeometry(QtCore.QRect(110, 20, 231, 20))
>         self.lineEdit_fileName.setObjectName("lineEdit_fileName")
>         self.listWidget_keys = QtWidgets.QListWidget(self.centralwidget)
>         self.listWidget_keys.setGeometry(QtCore.QRect(110, 100, 131, 141))
>         self.listWidget_keys.setObjectName("listWidget_keys")
>         self.comboBox_chooseSheet = QtWidgets.QComboBox(self.centralwidget)
>         self.comboBox_chooseSheet.setGeometry(QtCore.QRect(110, 50, 231, 22))
>         self.comboBox_chooseSheet.setObjectName("comboBox_chooseSheet")
>         MainWindow.setCentralWidget(self.centralwidget)
>         self.menubar = QtWidgets.QMenuBar(MainWindow)
>         self.menubar.setGeometry(QtCore.QRect(0, 0, 440, 19))
>         self.menubar.setObjectName("menubar")
>         MainWindow.setMenuBar(self.menubar)
>         self.statusbar = QtWidgets.QStatusBar(MainWindow)
>         self.statusbar.setObjectName("statusbar")
>         MainWindow.setStatusBar(self.statusbar)
> 
>         self.retranslateUi(MainWindow)
>         QtCore.QMetaObject.connectSlotsByName(MainWindow)
> 
>     def retranslateUi(self, MainWindow):
>         _translate = QtCore.QCoreApplication.translate
>         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
>         self.label_keyName.setText(_translate("MainWindow", "Key:"))
>         self.label.setText(_translate("MainWindow", "Source:"))
>         self.pushButton_chooseFile.setText(_translate("MainWindow", "PushButton"))
>         self.label_2.setText(_translate("MainWindow", "Sheets:"))
>         self.label_Headers.setText(_translate("MainWindow", "Fields:"))
> 
> 
> 
> 
> 
> <test_with_load_uic.py>
> <testmainwindow.ui>
> <testmainwindow_ui.py>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt



More information about the PyQt mailing list