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

Hans Jörg Maurer hjm at pmeonline.net
Thu Mar 7 18:42:25 GMT 2019


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:




 MainWindow
 
  
   
    0
    0
    440
    310
   
  
  
   MainWindow
  
  
   
    
     
      110
      80
      43
      13
     
    
    
     Key:
    
   
   
    
     
      50
      20
      43
      13
     
    
    
     Source:
    
   
   
    
     
      350
      20
      80
      20
     
    
    
     PushButton
    
   
   
    
     
      50
      50
      43
      13
     
    
    
     Sheets:
    
   
   
    
     
      70
      240
      341
      32
     
    
    
     Qt::Horizontal
    
    
     QDialogButtonBox::Cancel|QDialogButtonBox::Ok
    
   
   
    
     
      250
      100
      151
      141
     
    
   
   
    
     
      250
      80
      43
      13
     
    
    
     Fields:
    
   
   
    
     
      110
      20
      231
      20
     
    
   
   
    
     
      110
      100
      131
      141
     
    
   
   
    
     
      110
      50
      231
      22
     
    
   
  
  
   
    
     0
     0
     440
     19
    
   
  
  
 
 
 




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:"))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190307/9984734f/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_with_load_uic.py
Type: application/x-python-code
Size: 1041 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190307/9984734f/attachment-0002.pyc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testmainwindow.ui
Type: application/xml
Size: 3631 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190307/9984734f/attachment-0001.wsdl>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testmainwindow_ui.py
Type: application/x-python-code
Size: 3831 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190307/9984734f/attachment-0003.pyc>


More information about the PyQt mailing list