Embed a file line by line in Combo Box in Python Qt - python

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.

Related

Style file not found in the style library and input is not a valid URL or path, when importing

I have a customized matplotlib style file, called "mystyle.mplstyle".
In the same folder, I have test.py with the following code:
import matplotlib.pyplot as plt
plt.style.use('mystyle.mplstyle')
That test.py file runs fine.
But if in a second test2.py file in the same folder, I run the following code:
import test
It would generate the following error messages:
Traceback (most recent call last):
File "C:/Users/.../test2.py", line 1, in
import test
File "C:\Users...\test.py", line 3, in
plt.style.use('mystyle.mplstyle')
File "C:\ProgramData\Anaconda3\envs...\lib\site-packages\matplotlib\style\core.py", line 130, in use
raise IOError(
OSError: 'mystyle.mplstyle' not found in the style library and input is not a valid URL or path; see style.available for list of available styles
Why is that? How to fix the import?
Many thanks!!

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

Does python have double import such as C/C++?

I have a class that I named Ui_Materials defined in materialsFrame.py When I run the following import in the given file:
from common.interface.interface import ShowHide
I get the following traceback:
Traceback (most recent call last):
File "./main.py", line 110, in <module>
main()
File "./main.py", line 91, in main
interfaceObj.showMaterials()
File "/home/mohsen/codes/amlak/amlak/src/common/interface/interface.py", line 80, in showMaterials
self.ui = Ui_Materials()
NameError: global name 'Ui_Materials' is not defined
Notes:
I have a function in interfaces that uses Ui_materials
When I comment import line, everything is OK.
Question: How can I solve my problem? I need to import line.
You need to import module that contains Ui_Materials class into ShowHide module.
Be aware that in python you can't perform circular import!! (if first module imports second one, second module must not import first one; also, if first imports second, and third imports first, that means that first and second must not import third one)

Categories

Resources