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
Related
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`
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
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
I am a newbie pyqt4 learner and i am a facing a problem while linking .py files with each other
def sigwin(self):
self.signUpWindow = QtGui.QDialog()
self.ui = Ui_Dialog2()
self.ui.setupUi(self.signUpWindow)
self.signUpWindow.show()
this is coding of my main.py file which is a window with a sign up button when i click on sign up it gives me error like this
Traceback (most recent call last):
File "main.py", line 40, in sigwin
self.ui.setupUi(self.signUpWindow)
File "/root/folder/abc/sign.py", line 80, in setupUi
a.clicked.connect(ui.signwin)
NameError: global name 'ui' is not defined
my main file is linked with sign.py file on which i think its giving me problem on the function which is linked with another file
def signwin(self):
self.welcomeWindow = QtGui.QDialog()
self.ui = Ui_Dialog1()
self.ui.setupUi(self.welcomeWindow)
self.welcomeWindow.show()
a=self.pushButton
a.clicked.connect(ui.signwin)
this a=self code is the signup button linked with signwin
its giving me error of global ui idk how to solve this
hope it helps
I am having a situation in which I have to fill Combo Box with data available in a file. Mine approach is
self.cmbBusListBox.addItem("Select ..")
lines = [line.rstrip('\n') for line in open('i2coutput.cfg')]
for line in lines:
self.cmbBusListBox.addItem(line)
self.cmbBusListBox.currentIndexChanged.connect(self.selectBusChange)
This process giving me error:
Traceback (most recent call last):
File "I2CMain.py", line 3, in <module>
from Py4 import QtGui, QtCore
ImportError: No module named Py4
In any file handling process for data populating from file in Combo Box giving same error.
Please guide me. Thanks in advance.
No module named Py4 but there is one named PyQt4:
from PyQt4 import QtGui, QtCore
It's just a typo in import statement and has nothing with combobox populating.