ImportError: No module named 'PyQt4' from .Elf file - python-3.x

i make test.py using pyqt, run python test.py, it run properly but after i make .Elf file it shows error Import-error: No module named 'PyQt4'
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
i want to make elf file of this test.py

Related

How to run new process in a PyQt5 application

I am trying to create new process to run separate tasks using multiprocessing. below is a simple demo:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from multiprocessing import Process as mProcess
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
self.initUI()
self.processes = []
def initUI(self):
self.setGeometry(200, 200, 300, 300)
self.setWindowTitle("Tech With Tim")
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("click me!")
self.b1.clicked.connect(self.button_clicked)
def dummyFunction(self):
def f(text):
print(text)
p = mProcess(target=f, args=(100,))
self.processes.append(p)
p.start()
def button_clicked(self):
self.dummyFunction()
def window():
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
window()
But I got this error:
Can't pickle local object 'MyWindow.dummyFunction.<locals>.f'
.
.
OSError: [WinError 6] The handle is invalid

pyqt keypress event in lineedit

i have created a untitled ui where exists one lineedit.
now i want to get the value of whatever i type in lineedit immediately
i figured out it could be done using keypressevent but i exactly didn't understand how to use it in lineedit now
from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow, Ui_Dialog):
def event(self, event):
if type(event) == QtGui.QKeyEvent:
print (event.key())
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
#here i want to get what is keypressed in my lineEdit
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
this is my untitled.py code that i have imported
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(662, 207)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 30, 113, 27))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
If you want to get the whole text in the line-edit as it is entered, you can use the textEdited signal. However, if you just want to get the current key that was pressed, you can use an event-filter.
Here is how to use both of these approaches:
class MainWindow(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.lineEdit.installEventFilter(self)
self.lineEdit.textEdited.connect(self.showCurrentText)
def eventFilter(self, source, event):
if (event.type() == QEvent.KeyPress and
source is self.lineEdit):
print('key press:', (event.key(), event.text()))
return super(MainWindow, self).eventFilter(source, event)
def showCurrentText(self, text):
print('current-text:', text)

Show sub window in main window

Can't work out how to embed a window in a main window using classes:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Qt4 tutorial using classes
This example will be built
on over time.
"""
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, MainWindow):
super(Form, self).__init__()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This is supposed to be the single most basic bit of code using classes. How do I get the second window to show please.
As ekhumoro already pointed out, your widget needs to be a child of your mainWindow. However, I do not think that you need to call show for the widget, since it anyways gets called as soon as its parent (MainWindow) calls show. As mata pointed out correctly, the proper way to add a Widget to a MainWindow instance is to use setCentralWidget. Here is a working example for clarification:
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, parent):
super(Form, self).__init__(parent)
self.lbl = QtGui.QLabel("Test", self)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.setCentralWidget(new_window)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Python pyqt interface code not running

I have written a python 3.4 code which uses pyqt4 GUI modules but when i run the module it does not show anything kindly help
import sys
from PyQt4 import QtGui,QtCore
class window(QtGui.QMainWindow):
def _init_(self):
super(Window, self)._init_()
self.setGeometry(50,50,500,300)
self.setWindowTitle("Tallman Server")
self.setWindowIcon(QtGui.QIcon("tracking.png"))
self.home()
def home():
btn=QtGui.QPushButton("Quit",self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.show()
def run():
app=QtGui.QApplication(sys.argv)
GUI=window()
sys.exit(app.exec_())
run()
Firstly, the function's name is __init__ instead of _init_.
Secondly, you have to add the self parameter to home().
Those changes will solve your problem.
Modified code:
import sys
from PyQt4 import QtGui,QtCore
class window(QtGui.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("Tallman Server")
self.setWindowIcon(QtGui.QIcon("tracking.png"))
self.home()
def home(self):
btn=QtGui.QPushButton("Quit",self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.show()
def run():
app=QtGui.QApplication(sys.argv)
GUI=window()
sys.exit(app.exec_())
run()

pyqt4 event programming (signal and slots )

I am trying to change this event code to standard signal-slot format.
But it does not work.
Can I get help?
this is slide number changing to lcd display number.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
vbox.addWidget(self.sld)
self.setLayout(vbox)
#sld.valueChanged.connect(lcd.display)
QtCore.QObject.connect(self.sld, QtCore.SIGNAL(str1), self, QtCore.SLOT("SHOW()"))
#QtCore.pyqtSlot()
def SHOW(str1):
self.lcd.display()
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You were very close with your first attempt. All you need is:
self.sld.valueChanged.connect(self.lcd.display)
(The other lines up to self.setGeometry can be deleted).

Resources