My development eviroment:
os: windows xp
python: python-3.1.2.msi
pyqt: PyQt-Py3.1-gpl-4.7.4-1.exe
code:
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
s = QtCore.QtString()
sys.exit(app.exec_())
It always show me
in 'module'
s = QtCore.QtString()
AttributeError: 'module' object has no attribute 'QtString'
I chaged code:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
app = QApplication(sys.argv)
s = QtString()
sys.exit(app.exec_())
Then it always show me like this:
in 'module'
s = QtString()
NameError: name 'QtString' is not defined
what should i do?
The issue is explained here http://inputvalidation.blogspot.com/2010/10/python3-pyqt4-and-missing-qstring.html
The reason why you couldn't load QString is that it is missing from PyQt4 (maybe earlier, who knows). Since Py3k, as opposed to Py2k, supports Unicode by default, there's no need in this class.
Instead of QString, for compatibility reasons, you should use this snippet somewhere around your import's:
try:
from PyQt4.QtCore import QString
except ImportError:
QString = str
Do you mean QString instead of QtString ?
(you can do help(QtCore) in the python interpreter and search for string)
Related
guys!
Trying to learn so PySide for Maya 2017 and from first step it was already a bit frustrating.
from PySide2 import QtGui
button = QtGui.QPushButton()
'module' object has no attribute 'QPushButton'
Anybody knows something about it ?
Try this method:
from PySide2.QtWidgets import *
button = QPushButton("Hello World")
button.show()
button = QtWidgets.QPushButton()
I am trying to create UI using PyQt4 on centos. But When ever I am trying to load QtGui.QMainWindow I am getting the error:
Traceback(most recent call last):
File "test.py", line 7, in <module>
Ui_MainWindow, QtBaseClass = uic.loadUiType(ui_file_path)
File "/usr/local/lib/python2.7/site-packages/PyQt4/uic/__init__.py", line 160, inloadUiType
return (ui_globals[winfo["uiclass"]],getattr(Qtgui, winfo["baseclass"]))
AttributeError:'module' object has no attribute 'qmainwindow'
This My Code,I am using python 2.7.9:
import sys
import os
from PyQt4 import QtCore, QtGui, uic
ui_file_path = os.getcwd()+"/test.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(ui_file_path)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Thanks in advance.
I was having the same issue on Ubuntu 16.04, using PyQt5, and python3. This could apply to PyQt4 as well, so why not give it a shot?
I found from https://doc.bccnsoft.com/docs/PyQt5/pyqt4_differences.html# the solution to be changing QtGui.QMainWindow to QtWidget.QMainWindow as they have changed. This also needed to be applied to QApplication as well, and the import of QtGui was not needed in the code (replaced with import ..., QtWidget).
I wish you luck in solving this issue.
Forgive me if I miss the point, but I don't understand why MyApp inherits from both QtGui.QMainWindow and Ui_MainWindow.
Would something like this work for you?
import sys
import os
from PyQt4 import QtGui, uic
ui_file_path = os.path.join(os.getcwd(), "test.ui") # Enter file here.
class MyApp(QtGui.QMainWindow):
def __init__(self):
# Parent class init
QtGui.QMainWindow.__init__(self)
# You may also write
# super(MyApp, self).__init__()
# Load UI
uic.loadUi(ui_file_path, self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Edit
There seems to be a case issue, here.
In my Python interpreter, I get this:
import os
os.PAth
# This prints: AttributeError: 'module' object has no attribute 'PAth'
os.path
# This works
In your case, you enter the correct case, but get an error with the attribute in an incorrect case in the error message:
class Myapp(QtGui.QMainWindow)
# AttributeError:'module' object has no attribute 'qmainwindow'
As if somewhere between the text editor and the interpreter, something had lowered the case of QMainWindow. I don't have any hypothesis regarding why this would happen, but it could be nested somewhere not obvious.
Can you reproduce in an interpreter? More explicitly, can you run a Python interpreter on the same machine and try this:
import PyQt4.QtGui
PyQt4.QtGui.QMainWindow
import os
os.PAth
and see what errors you get?
(Note: in your comment below, there's a case error in QtGui.QMainWindow (you wrote QtGui.QMainWIndow). I suppose you made it while writing the comment, not in the code.)
I am trying convert my code from PyQt4 to PyQt5 but I am getting errors.
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
Traceback (most recent call last):
File "C:\Python34\Projects\name.py", line 9, in <module>
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
AttributeError: type object 'QPixmap' has no attribute 'grabWindow'
You should use QScreen::grabWindow() instead. QPixmap::grabWindow() is deprecated in Qt 5.0 because:
there might be platform plugins in which window system identifiers (WId) are local to a screen.
grabWindow method is now available in QScreen class.
You need to create QScreen object, initialize it with ex. QtGuiApplication.primaryScreen() and then grab the screen
screen.grabWindow(QApplication.desktop().winId())
Full example for PyQt5
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')
I've used PyQt4 in maya quite a bit, and generally I've found it quite easy to switch to PySide, but I'm having trouble getting the pointer to the main window. Perhaps someone can understand what's going wrong.
Here's what I do in PyQt4:
import sip, PyQt4.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PyQt4.QtCore.QObject)
This works fine. When I try the same in PySide:
import sip, PySide.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PySide.QtCore.QObject)
I get the following error:
# Error: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# TypeError: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType #
Anyone know what's going wrong?
You need to import shiboken instead of sip and pass QWidget to wrapInstance instead of QObject
Edit: Maya2017 contains shiboken2 and PySide2 instead of shiboken and PySide as pointed out in comments below.
import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI
def getMayaWindow():
"""
Get the main Maya window as a QtGui.QMainWindow instance
#return: QtGui.QMainWindow instance of the top level Maya windows
"""
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QWidget)
Please note that sip has wrapinstance in which i is not capital but in shiboken.wrapInstance i is capital.
shiboken.wrapInstance() requires wrapertype as a second argument, so you can pass QWidget as a second argument.
Because the previous answer has become somewhat out-of-date, here is a more current version to save someone the trouble of having to update it themselves.
Two approaches for getting the maya main window:
using PySide2:
from PySide2 import QtWidgets
global app
app = QtWidgets.QApplication.instance() #get the qApp instance if it exists.
if not app:
app = QtWidgets.QApplication(sys.argv)
def getMayaMainWindow():
mayaWin = next(w for w in app.topLevelWidgets() if w.objectName()=='MayaWindow')
return mayaWin
print(getMayaMainWindow())
Using shiboken2 and the maya api:
import maya.OpenMayaUI as apiUI
from PySide2 import QtWidgets
try:
import shiboken2
except:
from PySide2 import shiboken2
def getMayaMainWindow():
ptr = apiUI.MQtUtil.mainWindow()
mayaWin = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)
return mayaWin
print(getMayaMainWindow())
depending on your needs, the following more modern code might be easier too use.
since maya now has a build in method to access the main window in qt
import pymel.core as pm
mayaWindow = pm.ui.Window("MayaWindow").asQtObject()
your_widget.setParent(mayaWindow)
My code:
import sys
import time
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import *
class Browser(QWebView):
def __init__(self):
QWebView.__init__(self)
self.loadFinished.connect(self._result_available)
def _result_available(self, ok):
doc = self.page().mainFrame().documentElement()
[...]
if __name__ == '__main__':
app = QApplication(sys.argv)
view = Browser()
view.load(QUrl('http://www.example.net/'))
app.exec_()
For some reason I get this error and I cannot figure it out why.
I have updated to the latest qtwebkit version and still I get this.
The QT manual said it was implemented in version 4.6 and I have qt version 4.6.2-26.el6_4.
I get the following error from the above code.
Traceback (most recent call last):
File "web.py", line 15, in _result_available
doc = self.page().mainFrame().documentElement()
AttributeError: 'QWebFrame' object has no attribute 'documentElement'
P.S. I also get this error since upgrading from qtwebkit version 2.0-3.el6 to 2.1.1-1.el6:
can't make "generic.orientation" because no QAccelerometer sensors exist
I had a similar issue and unfortunately I have discovered that there is a bug in the Centos package as far as I can tell. I have run a comparison of the contents of all distributions for that version and they do not match. I will wait for an update of Centos that seems to be a bit behind other distributions.