0

When I use QtDesigner and pyuic5 for PyQt5 GUI applications it always uses imports like this:

from PyQt5 import QtCore, QtWidgets


class Ui_TabWidget(object):
    def setupUi(self, TabWidget):
        TabWidget.setObjectName("TabWidget")
        TabWidget.resize(950, 188)
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
    ...

Is it possible to configure QtDesigner or pyuic5 to have it like this:

from PyQt5.QtWidgets import QWidget

I don't want to import everything from QtWidgets to keep my compiled binaries smaller. However, if I edit the file by myself it will lose all the changes if I change the GUI via QtDesigner in the future.

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41
  • What makes you think that *everything* is being imported from `QtWidgets`? And what do you mean by "compiled binaries"? – ekhumoro Jul 12 '17 at 20:04
  • I don't know. I'm beginner. When I compile my program with pyinstall isn't it better to have less import? When is exe smaller, from pyqt5 import qtwidgets or from pyqt5.qtwidgets import QWidget? – Hrvoje T Jul 12 '17 at 21:19
  • 1
    I have no idea - why not just try it and see? My hunch would be that it makes no difference, because, either way, the `QtWidgets` module will be added to `sys.modules`. – ekhumoro Jul 12 '17 at 22:57
  • I thought if I import QtWidgets it brings all other widgets with it. And importing from PyQt5.QtWidgets import QWidget imports only QWidget? – Hrvoje T Jul 12 '17 at 23:43
  • 1
    No, `from PyQt5 import QtWidgets` does **one** thing: it imports the `QtWidgets` module; whereas `from PyQt5.QtWidgets import QWidget` does **two** things: it imports the module, and also adds `QWidget` to the local namespace. To add *everything* to the local namespace, you'd need to do `from PyQt5.QtWidgets import *`. In general, using "star imports" is considered bad practice in python, because it may clutter the local namespace with many objects that you don't really need. – ekhumoro Jul 13 '17 at 06:33
  • Thank you, I learned something today. It didn't answer my question but I learned that my question is unneeded :) – Hrvoje T Jul 13 '17 at 06:51

0 Answers0