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

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:

Related

Qt Designer error with Python throwing error when I try to load image

I'm trying to load a logo into a python scripts UI. I'm using Qt Designer and I created a label and set pixmap to the image. The image loads fine in the designer but when I import the ui file into the python script I get this error message
C:\Users\Mason\AppData\Local\Continuum\anaconda3\python.exe "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py"
Traceback (most recent call last):
File "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py", line 9, in <module>
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
File "C:\Users\Mason\AppData\Local\Continuum\anaconda3\lib\site-packages\PyQt5\uic\__init__.py", line 201, in loadUiType
exec(code_string.getvalue(), ui_globals)
File "<string>", line 30
import 3_rc
I still get that error even if I take the image out of the ui file and reload it. What am I doing wrong?
from PyQt5 import QtCore, uic, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap
import sys
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
class MyApp(UIClass, QtBaseClass):
def __init__(self):
UIClass.__init__(self)
QtBaseClass.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushbutton_click)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Qt relies on a resource system for images and other assets. When you add an image in qt designer, it creates a resource file. You have to convert that resource file, just like with a ui file (uic is a shortcut to this process). The autogenerated ui file you get with uic is looking for that resource file, but can't find it.
You can convert your resource file to python with this, depending on your qt version:
pyrcc5 -o 3_rc.py your_resource_file_here.qrc
More info:
Related question: Importing resource file to PyQt code?
https://www.riverbankcomputing.com/static/Docs/PyQt4/resources.html
https://www.riverbankcomputing.com/static/Docs/PyQt5/resources.html

Import .ui during definition of QMainWindow

i'm trying to load a "mw.ui" file (made with Qt creator) as the design to be used on my Main Window.
I successfully load a class with:
from pyqtgraph.Qt import QtGui, QtCore, uic
mw = uic.loadUi("mw.ui")
but i can't define my class since I already have my object built. I tried to compile the mw.py during execution with
uic.compileUi("mw.ui", "mw.py")
but i have the following error:
Traceback (most recent call last):
File "myFile.py", line 26, in
uic.compileUi("mw.ui", "mw.py")
File "C:\Users\giovanni\AppData\Local\Programs\Python\Python36\lib\site-packages\PyQt5\uic__init__.py", line 162, in compileUi
pyfile.write(_header % (uifname, PYQT_VERSION_STR))
AttributeError: 'str' object has no attribute 'write'
I read that i can use pyuic4 to convert the ui to a py file but doing so every small change need a long work (compiling, etc)
I tried to "save as" with qtCreator but i can't find the PY in the infinite list of available formats
Is there a way to load the UI file in "class definition phase" without converting with pyuic4 every single changes? For me it will be ok to implement a script that compile at the begin with uic.compileUi or something similar
Thanks
I get it with
class MainW(QtGui.QMainWindow):
def __init__(self, *args):
super(MainW, self).__init__(*args)
uic.loadUi("mw.ui", self)
from: http://pyqt.sourceforge.net/Docs/PyQt4/designer.html
PyQt4.uic.loadUi(uifile[, baseinstance=None[, package=”[, resource_suffix=’_rc’]]])
baseinstance – the optional instance of the Qt base class. If
specified then the user interface is created in it. Otherwise a new
instance of the base class is automatically created.

How to connect PyQt signal to external function

How does one connect a pyqt button signal in one file, to a function in another python file? I've tried various things, but nothing seems to work.
This is the first file:
from PyQt4 import QtGui
from PyQt4.QtGui import QMainWindow
from MainUIFile import Ui_Main
from pythonfile import myOutsideFunction
class MainWindow(QMainWindow,Ui_file):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.btn.clicked.connect(myOutsideFunction())
The second file that is called by the first:
def myOutsideFunction(self):
# Do some stuff here
How would I go about doing this?
You are currently making a call to myOutsideFunction and passing the result to the connect function, which expects a callable as an argument.
Remove the parenthesis from myOutsideFunction in the connect call
self.btn.clicked.connect(myOutsideFunction)
What is the importance of connecting to a function outside of your code? Could you not just do something like this:
def myOutsideFunction(a,b,c,d):
#process variables/objects a,b,c,d as you wish
return answer
from PyQt4 import QtGui
from PyQt4.QtGui import QMainWindow
from MainUIFile import Ui_Main
from pythonfile import myOutsideFunction
class MainWindow(QMainWindow,Ui_file):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.btn.clicked.connect(self.pressed_the_button())
#initialize your variables/objects for your outside function if need be
def pressed_the_button(self):
answer_from_outside_function = myOutsideFunction(self.a,self.b,self.c,self.d)
# run whatever you need to here...

'module' object has no attribute 'qmainwindow'

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

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.

Categories

Resources