'module' object has no attribute 'qmainwindow' - python

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.)

Related

pyqt5, Receiving AttributeError: 'QMainWindow' object has no attribute 'browseSlot'

I'm learning pyqt5, and specifically how to use it with the QT Designer. I'm sort of following the turorial HERE. However in this tutorial they are converting the XML interface to Python code with pyuic5, while I'm trying to import it dynamically with uic.loadUi("myui.ui"). In the tutorial we define a slot with the signals and slot editor named " browseSlot".
When I try to run/compile, at the line
dlg = uic.loadUi("myui.ui")
I get the error:
AttributeError: 'QMainWindow' object has no attribute 'browseSlot'
I think what's going on is that QT Designer connects a signal to the slot 'browseSlot' but because a 'browseSlot' method isn't defined in the myui.ui, the error is thrown, because there is no way for the interpreter to know I'm referring to a method that is outside the UI interface file. (In this case, in the module that loads the interface). As far as I can tell QT Designer only lets me connect signals to slots, not define a whole new one. I think that way this is handled in other frameworks is that there will be an abstract method that needs over riding. So what can I do in this situation to make it work?
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import QObject, pyqtSlot
import sys
app = QtWidgets.QApplication([])
dlg = uic.loadUi("myui.ui")
#pyqtSlot
def returnPressedSlot():
pass
#pyqtSlot
def writeDocSlot():
pass
#pyQt
def browseSlot():
pass
dlg.show()
sys.exit(app.exec())
The slots belong to the class that is used returns loadUi(), they are not any functions since they do not magically not connect them, if you want to use loadUi() and implement these methods you must inherit from the class corresponding to the template that you used, in the example of the link Main Window was used so it must be inherited from QMainWindow:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
uic.loadUi("mainwindow.ui", self)
#QtCore.pyqtSlot()
def returnPressedSlot():
pass
#QtCore.pyqtSlot()
def writeDocSlot():
pass
#QtCore.pyqtSlot()
def browseSlot():
pass
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
try this out
from PyQt5 import QtWidgets, uic
app = QtWidgets.QApplication([])
form = uic.loadUi("login.ui")
form2.show()
app.exec()
the above python code should display your gui app properly as long as you have install PyQt5 and PyQt5-tools,if you haven't then open CMD and typeenter code here "pip install PyQt5" and click enter.once installation is done type "pip install PyQt5-tools" then you are good to go

Pyqt5, AttributeError: module 'x_ui' has no attribute 'Ui_x'

Hello I have a QTDesigner UI file HelloWorld.ui which I am trying to import into a project and execute.
The Project includes HelloWorld.ui file which has been converted into HelloWorld_ui.py using Pyuic5.
The following is the code of app.py
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import HelloWorld_ui
class HelloWorld(QDialog, HelloWorld_ui.Ui_HelloWorld):
def __init__(self):
QDialog.__init__(self)
self.setupUi(self)
app = QApplication(sys.argv)
helloworld = HelloWorld()
helloworld.show()
app.exec_()
The following is the error code
Traceback (most recent call last):
File "/Users/rrpolak/Downloads/Pyt/app.py", line 11, in <module>
class HelloWorld(QDialog, HelloWorld_ui.Ui_HelloWorld):
AttributeError: module 'HelloWorld_ui' has no attribute 'Ui_HelloWorld'
Process finished with exit code 1
I am trying to understand what is the correct way to call these files in the python program. Any help is appreciated.
The project file is at https://drive.google.com/open?id=18tjLPiCZxTbKaiZShtgu90KyXcFukr6V
I am using PyQt5/Python3.6/Mac.
If you check the file HelloWorld_ui.py you will notice that there is no class called Ui_HelloWorld, but the class Ui_Dialog:
class Ui_Dialog(object):
so you must use that class:
class HelloWorld(QDialog, HelloWorld_ui.Ui_Dialog):
The name is generated by the name you give to QDialog:
If you want to use HelloWorld you must change it:
convert the .ui to .py again and execute it again obtaining the following:

How to get maya main window pointer using PySide?

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)

QWebFrame object has no attribute documentElement

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.

Accesing PyQt Gui elements from another file

I am learning PyQt and coming from webdesign, so excuse this question that must have very obvious answer.So I am building a PyQt application and I would like to spread methods to several files to correspond different parts of GUI. How can I access textbox locating in fileA.py from fileB.py. :
#fileA.py
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB
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()
#This works all fine
def pressed():
window.plainTextEdit.appendPlainText("Hello")
window.pushButton.pressed.connect(pressed)
window.button2.pressed.connect(fileB.func3)
sys.exit(app.exec_())
Now, in this file I would like to use textbox from fileA.py
#fileB.py
import fileA
#How do I access window.plainTextEdit from fileA.py
def func3():
print "hello"
fileA.window.plainTextEdit.appendPlainText("Hello")
What am I doing wrong? What would be best way to spread functionality to multiple files if not this?
Thank you for taking time to read this.
You can take advantage of Python's class inheritance, like so:
fileA.py:
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB
class MyApp(fileB.MyApp, QtGui.QMainWindow):
def __init__(self):
self.MyMethod()
# Should print 'foo'
fileB.py:
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
class MyApp(QtGui.QMainWindow):
def MyMethod(self):
print 'foo'
Well, first off, the code under if __name__ == "__main__" will never be run when you are importing fileA.py, and so fileA.window does not exist. That's what it should do: run only when __name__ is "__main__", i.e. run as a top-level program. Instead, you should import fileA.py, create the QApplication and window again, then access window.plainTextEdit. However, this creates a very tight coupling between the code, as you are directly accessing a widget in MyApp from fileB. It might be better if instead, you expose a method in MyApp that appends to the text box instead of having fileB.py do it directly. So you may want to think about what you want to do and how to structure your program.
Of course, you don't have to structure your code that way; you could simply do
window = MyApp()
window.plainTextEdit.appendPlainText("Hello")
in fileB if you wanted.

Categories

Resources