Runtime Error while using Rviz in a PyQT application - python

I am trying to use Rviz (ROS Visualization tool) in my own application written in PyQt4. I am using QTcreator IDE to design the forms and convert it into python code using pyuic4.
Rviz library uses python_qt_binding which inturn uses PyQT or PySide (in my case I have only installed PyQt).
I am trying to initialize Rviz as a QWidget and display it in my application. My application and Rviz widget work fine when run individually, but shows the following error when I try to Import Rviz QWidget into the application.
Traceback (most recent call last):
File "./mainwindow.py", line 38, in <module>
from RvizInitializer import RViz
File "/home/parallels/Documents/eclipse/workspace/GUI/src/RvizInitializer.py", line 8, in <module>
from python_qt_binding.QtGui import *
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/__init__.py", line 55, in <module>
from .binding_helper import loadUi, QT_BINDING, QT_BINDING_MODULES, QT_BINDING_VERSION # #UnusedImport
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 252, in <module>
getattr(sys, 'SELECT_QT_BINDING_ORDER', None),
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 89, in _select_qt_binding
QT_BINDING_VERSION = binding_loader(required_modules, optional_modules)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 131, in _load_pyqt
_named_import('PyQt5.%s' % module_name)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 111, in _named_import
module = builtins.__import__(name)
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
code snippets:
Rviz initializer
class RViz():
def __init__(self):
self.Mainwidget = QWidget()
self.frame = rviz.VisualizationFrame()
self.frame.setSplashPath( "" )
self.frame.initialize()
reader = rviz.YamlConfigReader()
config = rviz.Config()
reader.readFile( config, "/home/parallels/catkin_ws/src/map-toddler/configuration_files/rviz_config_files/karto.rviz" )
self.frame.load( config )
self.Mainwidget.setWindowTitle( config.mapGetChild( "Title" ).getValue() )
self.frame.setMenuBar( None )
self.frame.setStatusBar( None )
self.frame.setHideButtonVisibility( False )
## Here we create the layout and other widgets in the usual Qt way.
layout = QVBoxLayout()
layout.addWidget( self.frame )
self.Mainwidget.setLayout(layout)
#self.Mainwidget.resize(500,500)
#self.Mainwidget.show()
Importing Rviz Qwidget:
from RvizInitializer import RViz
class mainwindow():
def __init__(self):
.
.
other code
.
.
self.RviZ = RViz()
self.MappingFrameHndl.SLAMLayout.addWidget(self.RviZ.Mainwidget)
Any help is greatly appreciated

Related

Cx_freeze converted executable is not working for ttkbootstrap scripts

After converting my ttkbootstrap project file into exe by using cx_freeze.
When I run the executable file. I get this error and my program does not execute.
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 120, in run
module_init.run(name + "__main__")
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\Lib\site-packages\cx_Freeze\initscripts\Console.py", line 16, in run
exec(code, module_main.__dict__)
File "main.py", line 207, in <module>
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\lib\site-packages\ttkbootstrap\window.py", line 273, in __init__
self._style = Style(themename)
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\lib\site-packages\ttkbootstrap\style.py", line 488, in __init__
localization.initialize_localities()
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\lib\site-packages\ttkbootstrap\localization\msgs.py", line 9, in initialize_localities
m.initialize()
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\lib\site-packages\ttkbootstrap\localization\msgs.py", line 27, in initialize
MessageCatalog.set_many(self.locale, *messages)
File "C:\Users\KANWAR\AppData\Local\Programs\Python\Python310\lib\site-packages\ttkbootstrap\localization\msgcat.py", line 142, in set_many
return int(root.tk.eval(out))
_tkinter.TclError: invalid command name "::msgcat::mcmset"
In my script I had included following library files.
from ttkbootstrap import *
from ttkbootstrap.constants import *
from ttkbootstrap.dialogs.dialogs import Messagebox
I'm facing this issue everytime. Please is there any solution for this.
Here's my setup.py
from cx_Freeze import setup, Executable
import sys
base = None
if (sys.platform == "win32"):
base = "Win32GUI" # Tells the build script to hide the console.
setup(name = "e_mail",
version = "0.1",
description = 'PURF BY KANWAR ADNAN',
executables = [Executable(r"main.py")]
)
Below is not a real solution.
Only first aid.
In your environment's Bootstrap folder (site-packages\ttkbootstrap\localization),
Please rewrite msgcat.py as follows.
from ttkbootstrap.window import get_default_root
class MessageCatalog:
#staticmethod
def translate(src):
return src
#staticmethod
def locale(newlocale=None):
return newlocale
#staticmethod
def preferences():
return []
#staticmethod
def load(dirname):
return 0
#staticmethod
def set(locale, src, translated=None):
pass
#staticmethod
def set_many(locale, *args):
return 1
#staticmethod
def max(*src):
return 1
This operation has the side effect of disabling the ability to switch languages.
Well, I just installed auto-py-to-exe and converted my script into exe without any problems.
Also, I used the virtual environment term. I installed virtualenv and made a virtual environment and that helped me reducing 450+ MBs of size my application.
Because auto-py-to-exe includes almost all the libraries in the python lib.

pyqt4 problems with resorce system (TypeError: qRegisterResourceData)

When I run this code (which I created a module for my qrc file)
import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
mainwindow = uic.loadUiType("mainmenu2.ui")[0] #load the UI
class Mainmenu(QtGui.QMainWindow, mainwindow):
def __init__(self, parent=None):
super(Mainmenu, self).__init__(parent)
self.setupUi(self)
#self.aboutp.clicked.connect(self.linkabout)
app = QtGui.QApplication(sys.argv)
mainwindow = Mainmenu
#aboutwindow = About_us_page(None)
mainwindow.show()
app.exec_()
I get this error
Traceback (most recent call last): File "C:\Users\megan\Desktop\newqt\try1.py", line 7, in <module>
mainwindow = uic.loadUiType("mainmenu2.ui")[0] #load the UI File "C:\Python34\lib\site-packages\PyQt4\uic\__init__.py", line 211, in loadUiType
exec(code_string.getvalue(), ui_globals) File "<string>", line 202, in <module> File "C:\Users\megan\Desktop\newqt\test_rc.py", line 49680, in <module>
qInitResources() File "C:\Users\megan\Desktop\newqt\test_rc.py", line 49675, in qInitResources
QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data) TypeError: qRegisterResourceData(int, str, str, str): argument 2 has unexpected type 'str`'
so I know the problem is in my resource file, and I've looked online and the only help I could find was this comment
pyrcc generates Python 2.x code by default.
Try regenerating your resource files using pyrcc with flag '-py3'
But I don't know how to do that so please if someone could help me solve this problem, thanks
Using powershell or the cmd go to the directory where the resource file is stored and run:
`pyrcc4 resource.qrc -o resource_rc.py -py3`

Error running py2app generated .app, QtWidgets.so "Reason Image not found"

I am running into a very frustrating error that occurs when trying to run my python application (.app) generated by py2app – on my 10.12.6 OS X laptop. PyQt5 was installed using pip3, and has been re-installed since the problem. This is the full error code:
Last login: Mon Oct 23 00:14:00 on ttys001
/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/MacOS/main ; exit;
Zacharys-MacBook-Pro:~ development$/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/MacOS/main ; exit;
Traceback (most recent call last):
File "/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/Resources/__boot__.py", line 386, in <module>
_run()
File "/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/Resources/__boot__.py", line 370, in _run
exec(compile(source, path, 'exec'), globals(), globals())
File "/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/Resources/main.py", line 4, in <module>
from sheet import Sheet
File "sheet.pyc", line 8, in <module>
File "PyQt5/QtWidgets.pyc", line 14, in <module>
File "PyQt5/QtWidgets.pyc", line 10, in __load
File "imp.pyc", line 342, in load_dynamic
ImportError: dlopen(/Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/Resources/lib/python3.6/lib-dynload/PyQt5/QtWidgets.so, 2):
Library not loaded: #rpath/QtWidgets.framework/Versions/5/QtWidgets
Referenced from: /Users/development/Desktop/python_workspace/BasicEmail/dist/main.app/Contents/Resources/lib/python3.6/lib-dynload/PyQt5/QtWidgets.so
Reason: image not found
2017-10-23 00:19:19.853 main[31651:3648399] main Error`
My code for the sheet.py. The first view of my application:
import sys
import os
import re
import io
import csv
#from PyQt5 import QtWidgets, QtCore, QtGui
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Sheet(QWidget):
buttonClicked = pyqtSignal()
email_list = []
#company_list = []
def __init__(self,r,c):
super().__init__()
self.init_ui(r,c)
def init_ui(self,r,c):
self.v_layout = QVBoxLayout()
self.h_layout = QHBoxLayout()
self.sheet = QTableWidget()
self.sheet.setRowCount(r)
self.sheet.setColumnCount(c)
item = QTableWidgetItem('Upload a CSV file with emails.')
self.sheet.setItem(0, 0, item)
self.sheet.resizeRowsToContents()
self.sheet.verticalHeader().setDefaultSectionSize(50)
width = 200
self.open_button = TemplateButton(QPixmap('res/open-folder.png'))
self.open_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.continue_button = TemplateButton(QPixmap('res/forward-arrow.png'))
self.continue_button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.file_path = None
self.open_button.clicked.connect(lambda: self.open_click(self.open_button,"Open",self.sheet))
self.continue_button.clicked.connect(lambda: self.continue_click(self.continue_button,"Continue"))
self.v_layout.addWidget(self.sheet)
self.h_layout.addWidget(self.open_button)
self.h_layout.addWidget(self.continue_button)
self.v_layout.addLayout(self.h_layout)
self.setLayout(self.v_layout)
self.setWindowTitle("EMAIL")
self.show()
Main I suspect the same other to be called on my others:
import sys
import os
import io
from sheet import Sheet
from email_list import EmailList
from construct import ConstructEmail
from construct import NewTemplateDialog, ConfirmDialog
from template_list import TemplateList, TemplateEdit
from settings import Settings
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
I have found an answer to the problem. While bundling your application you have to include the flag --packages=PyQt5
This worked for me:
python setup.py py2app --packages=PyQt5

Name Error: name 'QFileDialog' is not defined

I am working on coursework for computer science and can't work out why the piece of code isn't working. I am trying to connect a button that I've created in PyQt4 so that when it is pressed it shows a directory dialogue:
self.Browse_Button_1 = QtGui.QToolButton(self.tab)
self.Browse_Button_1.setGeometry(QtCore.QRect(360, 30, 61, 20))
self.Browse_Button_1.setObjectName(_fromUtf8("Browse_Button_1"))
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
self.Browse_Button_1.clicked.connect(self, file)
However every time I run the program I just get this error:
Traceback (most recent call last):
File "D:\NEA Project\NEA_UI.py", line 194, in <module>
ui = Ui_Dialog()
File "D:\NEA Project\NEA_UI.py", line 30, in __init__
self.setupUi(self)
File "D:\NEA Project\NEA_UI.py", line 55, in setupUi
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
NameError: name 'QFileDialog' is not defined
Any help with the problem would be greatly appreciated.
QFileDialog is in the QtGui module, so you need to append that to beginning of your line, e.g.:
file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
Alternatively, if you want to use QFileDialog without the QtGui in front, you need to import it from the module (at the top of your file), with:
from PyQt4.QtGui import QFileDialog
Or for Qt5 (note that in Qt5, QFileDialog moved to the QtWidgets module):
from PyQt5.QtWidgets import QFileDialog

Tkinter Python Image error

I"m confused as to why I'm getting this error. I have looked into the file specified in the error as well as done some research on PIL and the actual error. Any help would be appreciated. This code is an example code, it doesn't belong to me. I'm following a tutorial I'm trying to learn a new gui module for python.
Code:
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Picture")
self.pack(fill=BOTH, expand=1)
Style().configure("TFrame", background="#333")
bard = Image.open("test.jpg")
bardejov = ImageTk.PhotoImage(bard)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x=20, y=20)
def main():
root = Tk()
root.geometry("300x280+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
enter code heremain()
Error:
Traceback (most recent call last):
File "C:/Python27/pics.py", line 36, in <module>
main()
File "C:/Python27/pics.py", line 31, in main
app = Example(root)
File "C:/Python27/pics.py", line 12, in __init__
self.initUI()
File "C:/Python27/pics.py", line 22, in initUI
bardejov = ImageTk.PhotoImage(bard)
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 116, in __init__
self.paste(image)
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 181, in paste
import _imagingtk
ImportError: DLL load failed: %1 is not a valid Win32 application.
"ImportError: DLL load failed: %1 is not a valid Win32 application." is from Windows itself, and means that your PIL or Tkinter install doesn't work on your Windows version.
One potential cause for this is that you're using a version built with VS 2012 on Windows XP; see:
http://blogs.msdn.com/b/vcblog/archive/2012/06/15/10320645.aspx

Categories

Resources