This question already has answers here:
How can you easily select between PyQt or PySide at runtime?
(3 answers)
Closed 8 years ago.
I'd like to use either PyQt4 or PySide for imports, whichever is installed. For some reason though I can't do this:
from PyQt4 import QtGui
from QtGui import QApplication
Instead of the last line, I have to do this, I'm not sure why:
from PyQt4.QtGui import QApplication
That doesn't bother me too much, but it makes it very frustrating importing multiple things using whichever library is installed:
try:
from PyQt4 import QtGui, QtWebKit, QtCore
from PyQt4.QtGui import QApplication, QMainWindow, QFrame, QAction, ...
...
except ImportError:
from PySide import QtGui, QtWebKit, QtCore
from PySide.QtGui import QApplication, QMainWindow, QFrame, QAction, ...
...
It gets pretty repetitive.
Also, this doesn't work, Python won't allow it:
import PyQt4 as SomeQt
from SomeQt import QtGui
So I can't find a good way to cut down on repetition between the PyQt4 and PySide sections. Is there a better way? Is there any harm in just using import *:
try:
from PyQt4 import QtGui, QtWebKit, QtCore
from PyQt4.QtGui import *
...
except ImportError:
from PySide import QtGui, QtWebKit, QtCore
from PySide.QtGui *
...
to at least cut down on the amount of repetition necessary?
I would just do following:
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
...
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
...
...
QThread()
QApplication()
QWidget()
...
Wild imports are usually discouraged by Python community and best practice. The reasoning is that you are unnecessarily polluting your namespace. Me personally, I do not care for Qt, as every class name is starting with Q, e.g. QSomething. This makes it in my opinion very unlikely to collide with another class names from my or 3rd party modules.
To be safe, you can also do something like:
try:
from PyQt4 import QtCore as Qc
from PyQt4 import QtGui as Qg
...
except ImportError:
from PySide import QtCore as Qc
from PySide import QtGui as Qg
...
...
Qc.QThread()
Qc.QApplication()
Qg.QWidget()
...
Related
how to make a EXE library much smaller in size and the exe startup faster?. My library is actually 580 MB and the startup time from the exe is around 10 seconds! Here is my main.py. Its imports data from interface. (sub program)
import sys
from datetime import datetime
from typing import Tuple
import numpy as np
import pandas as pd
from PyQt5.Qt import Qt
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtWidgets import (QWidget, QTableWidget, QTableWidgetItem)
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtGui import QKeySequence
from PyQt5.QtGui import QMovie
from PyQt5.QtGui import QPainter
from PyQt5.QtGui import QPen
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import Qt, QPoint
import cv2
import os
from sys import path
import shutil
import xlsxwriter
from tkinter import Tk, PhotoImage, Canvas
from interface import *
from line_detection import *
import openpyxl
from openpyxl.styles import Alignment, Font
import os.path
The header of interface.py looks like this:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os.path
import pandas as pd
import numpy as np
from datetime import datetime
import xlsxwriter
import openpyxl
from openpyxl.styles import Alignment, Font
from openpyxl import load_workbook
In Inteface.py i do something like this:
MyApp = QtWidgets.QApplication(sys.argv)
V = MyApp.desktop().screenGeometry()
h = V.height()
w = V.width()
class Ui_Form(object):
def __init__(self):
self.show_label = True
self.show_groupbox = False
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(w, h)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.image_label1 = QtWidgets.QLabel(self.centralwidget)
self.image_label1.setGeometry(QtCore.QRect(0, 0, w, h))
self.image_label1.setText("")
self.image_label1.setObjectName("image_label1")
self.image_label1.setHidden(self.show_label)
self.image_label1.setDisabled(self.show_groupbox)
My cx_freeze (setup.py) looks like this. Its still including all packages and so on. How to make PyQT5 and other Packages much smaller when making a EXE
import sys
from cx_Freeze import setup, Executable
try:
from cx_Freeze.hooks import get_qt_plugins_paths
except ImportError:
include_files = []
else:
# Inclusion of extra plugins (new in cx_Freeze 6.8b2)
# cx_Freeze imports automatically the following plugins depending of the
# use of some modules:
# imageformats - QtGui
# platforms - QtGui
# mediaservice - QtMultimedia
# printsupport - QtPrintSupport
#
# So, "platforms" is used here for demonstration purposes.
include_files = get_qt_plugins_paths("PyQt5", "platforms")
# base="Win32GUI" should be used only for Windows GUI app
base = None
if sys.platform == "win32":
base = "Win32GUI"
build_exe_options = {
#[""]
"excludes": ["PyQt5.QtBluetooth",
"PyQt5.QtNetwork",
"PyQt5.QtNfc",
"PyQt5.QtWebChannel",
"PyQt5.QtWebEngine",
"PyQt5.QtWebEngineCore",
"PyQt5.QtWebEngineWidgets",
"PyQt5.QtWebKit",
"PyQt5.QtWebKitWidgets",
"PyQt5.QtWebsockets",
"PyQt5.QtSql",
"PyQt5.QtScript"],
"include_files": include_files,
}
bdist_mac_options = {
"bundle_name": "TEST",
}
bdist_dmg_options = {
"volume_label": "TEST",
}
executables = [Executable("main.py", base=base, target_name="TEST")]
setup(
name="TEST",
version="2.4",
description="TEST",
options={
"build_exe": build_exe_options,
"bdist_mac": bdist_mac_options,
"bdist_dmg": bdist_dmg_options,
},
executables=executables,
)
I am developping a PyQt5 gui.
I try to implement a push button that would be pushed when I hit a key on my keyboard. Let say 'K'.
I have tried this but it is not working. Any thoughts?
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout,QTabWidget, QFileDialog, QShortcut
from PyQt5.QtGui import QIcon, QPixmap, QKeySequence
from PyQt5 import QtCore, QtGui, QtWidgets, uic
Ui_TabWidget, QtBaseClass = uic.loadUiType(qtcreator_file)
class TabWidget(QTabWidget, Ui_TabWidget):
def __init__(self):
QtWidgets.QTabWidget.__init__(self)
Ui_TabWidget.__init__(self)
self.setupUi(self)
self.pushButtonGood.clicked.connect(lambda : self.writeOutliers(False))
self.pushButtonOutliers.clicked.connect(lambda : self.writeOutliers(True))
self.shortcut_open = QShortcut(QKeySequence('K'), self)
self.shortcut_open.activated.connect(lambda : self.writeOutliers(True))
from PyQt5.QtGui import QIcon, QPixmap, QKeySequence
from PyQt5 import QtCore, QtGui, QtWidgets, uic
Ui_TabWidget, QtBaseClass = uic.loadUiType(qtcreator_file)
class TabWidget(QTabWidget, Ui_TabWidget):
def __init__(self):
QtWidgets.QTabWidget.__init__(self)
Ui_TabWidget.__init__(self)
self.setupUi(self)
self.pushButtonGood.clicked.connect(lambda : self.writeOutliers(False))
self.pushButtonOutliers.clicked.connect(lambda : self.writeOutliers(True))
self.pushButtonGood.clicked.connect(self.setFocus)
self.setFocus get the keyboard focus elsewhere that edittext at the first click on pushButtonGood.
Then define the key shortcut of the buttons in the QTdesigner with the "Editeur de Propriétés".
Do a complexe shortcut 'ctrl+K'
I am new on Python and PyQt5.
I used Qt Design to create a form for input.
I am able to print the 3 data that the user input but I don't know how to store them in a variable so that I can use them in the main program.
import PyQt5 as pq
import sys
from PyQt5 import QtWidgets, uic, QtGui
def Assigned(self):
Nx=call.Nx11.text()
Ny=call.Ny11.text()
Nxy=call.Nxy11.text()
Mx=call.Mx11.text()
My=call.My11.text()
Mxy=call.Mxy11.text()
print(Nx)
print(Ny)
print(Nxy)
return(Nx)
return(Ny)
return(Nxy)
app=QtWidgets.QApplication([])
call=pq.uic.loadUi("InputLoad.ui")
call.OKbutton.clicked.connect(Assigned)
call.show()
app.exec_()
Thank you in advance for any help.
Fab
Create a global variable and store them
import PyQt5 as pq
import sys
from PyQt5 import QtWidgets, uic, QtGui
Nx = None
Ny = None
Nxy = None
def Assigned(self):
global Nx, Ny, Nxy
Nx=call.Nx11.text()
Ny=call.Ny11.text()
Nxy=call.Nxy11.text()
Mx=call.Mx11.text()
My=call.My11.text()
Mxy=call.Mxy11.text()
print(Nx)
print(Ny)
print(Nxy)
app=QtWidgets.QApplication([])
call=pq.uic.loadUi("InputLoad.ui")
call.OKbutton.clicked.connect(Assigned)
call.show()
app.exec_()
Trying to learn some PySide2 for Maya, but there is no clear documentation for PySide2 yet, so after searching in internet coming here again for help...
I will go straight to the problem -
from PySide2 import QtWidgets, QtGui
import maya.cmds as cmds
import maya.OpenMayaUI as mui
import shiboken2
def getMayaWindow():
pointer = mui.MQtUtil.mainWindow()
if pointer is not None:
return shiboken2.wrapInstance(long(pointer), QtWidgets)
Error: TypeError: file line 9: 'wrapInstance' called with wrong argument types:
wrapInstance(long, module)
Supported signatures:
wrapInstance(size_t, PyType) #
Best regards!
You should try to import modules by the following way:
import maya.cmds as cmds
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import maya.OpenMayaUI as omui
try:
from shiboken import wrapInstance
except:
from shiboken2 import wrapInstance
def getMayaWindow():
pointer = omui.MQtUtil.mainWindow()
if pointer is not None:
return shiboken2.wrapInstance(long(pointer), QWidget)
getMayaWindow()
QObject.inherits(className) does work different in PyQt5 than in PyQt4 and PySide.
from PyQt5 import QtWidgets
#from PySide import QtGui
#from PyQt4 import QtGui
QtWidgets = QtGui
class MyWidget(QtWidgets.QWidget):
pass
app = QtWidgets.QApplication([])
w = MyWidget()
print(w.inherits("MyWidget"))
In PyQt5 it prints False, while in PyQt4 and PySide (uncomment the second or third line and comment the first one) it prints True. Why is that and how to fix it?
I can confirm this behaviour in PyQt-5.7.
It seems to be a bug, because the same problem does not appear in the latest development snapshot (PyQt5_gpl-5.7.1.dev1611251257). The only solution is to wait until PyQt-5.7.1 is released.