PyQT badly crashing on a given website - python

I managed to create and thread a PyQt application to do some screenshots of a list of urls. It's working really good, but it's always crashing on some websites (or at least on for sure).
Softpedia.com is one of them. It's just crashing Python with a segmentation fault just before the loadFinished event. Even if I don't use my threaded application but "raw" PyQt.
I tested with basic PyQt based browser I found on the web and I got the same issue.
Does anybody have an idea? I'm starting to think that this may be a dead end, which is not really a good news.
Thank you all!

There's probably some kind of issue with the the code you are using, this code shouldn't raise errors:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtGui, QtWebKit
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.barProgress = QtGui.QProgressBar(self)
self.barProgress.setFixedSize(80, 15)
self.barProgress.setVisible(False)
self.barStatus = QtGui.QStatusBar(self)
self.barStatus.addPermanentWidget(self.barProgress)
self.webView = QtWebKit.QWebView(self)
settingsWeb = [
QtWebKit.QWebSettings.PluginsEnabled
]
for setting in settingsWeb:
self.webView.settings().setAttribute(setting, True)
self.setStatusBar(self.barStatus)
self.setCentralWidget(self.webView)
self.webView.loadFinished.connect(self.on_webView_loadFinished)
self.webView.loadFinished.connect(self.barProgress.hide)
self.webView.loadStarted.connect(self.barProgress.show)
self.webView.loadProgress.connect(self.barProgress.setValue)
#QtCore.pyqtSlot()
def on_webView_loadFinished(self):
QtGui.QMessageBox.information(None, "Info", "Load finished!")
def loadUrl(self, url):
self.webView.load(QtCore.QUrl(url))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
main.loadUrl("http://www.example.com/")
sys.exit(app.exec_())

Related

PyQt app crashes on exit (Python 3.4.3 / PyQt5 / Windows 10)

I realize this is a bit of a vague question, but I'm not sure how to make it more specific.
The skeleton for my code is below:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class FormWidget(QWidget):
def __init__(self, parent):
super(FormWidget, self).__init__(parent)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
form = FormWidget(self)
self.setCentralWidget(form)
[do a bunch of stuff with form]
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
Within the FormWidget class, there is a keyPressEvent() method that traps keyboard input, and on a specific key ('q'), calls this:
mainWindow.close()
Most of the time, that causes the app to exit, as desired. But occasionally (with no predictable pattern that I can discern), it causes a crash (dialog comes up that says 'python.exe has stopped working', with a 'Close program' button).
First question, I guess, is: Is this not the proper way to terminate a PyQt GUI app?
If it is, then second question is: Presumably, my code is misbehaving somewhere. But I'm not sure what sort of misbehavior to be looking for. What sort of mistake could I be making that would cause this?
Any thoughts appreciated.
/John

QPlainTextEdit thinks it's modified if it has an empty text

I'm using PyQt to build a simple IDE and getting weird errors if you load an empty file. A small example script is posted below:
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = QtGui.QPlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('a')
def _change_modified(self, have_change):
print(have_change)
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
As expected, this shows a window with a plain text editor. As soon as the setPlainText method is called, the editor emits two events: One modificationChanged event with changes=True, a second with changes=False.
A bit weird, but fine.
However, if you change setPlainText('a') to setPlainText(''), only a single event is emitted, this time with changes=True. Even worse, after telling the editor it's not modified with setModified(False), it insists it's been changed somehow.
Does anyone know what's causing this and how I can work around this issue?
Update: It seems to be a bug & also affects QPlainTextEdit.clear().
The workaround below places a wrapper around the QPlainTextEdit to fix clear() and setPlainText('').
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = PlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('')
def _change_modified(self, have_change):
print(have_change)
class PlainTextEdit(QtGui.QPlainTextEdit):
def clear(self):
self.selectAll()
cursor = self.textCursor()
cursor.removeSelectedText()
doc = self.document()
doc.clearUndoRedoStacks()
doc.setModified(False)
self.modificationChanged.emit(False)
def setPlainText(self, text):
if text:
super(PlainTextEdit, self).setPlainText(text)
else:
self.clear()
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
It's a Qt bug, and the straightforward workaround is to check for empty contents if modifications are indicated.

Python has stopped working in PyQt application

code like the following .After it running for about half a minute ,then close the window, then the "Python has stopped working" dialog pops up(you had better try more than once. )
I wonder why this happen ?any solution to this ?
​
Tested on Windows with PyQt4-4.11.3-gpl-Py3.4-Qt4.8.6-x32.exe
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowIcon(QIcon("./wa.png"))
self.createTrayIcon()
def createTrayIcon(self):
self.trayIcon = QSystemTrayIcon()
self.trayIcon.setIcon(self.windowIcon())
self.trayIcon.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
I do my PyQt work on linux, so there might be differences, but usually I construct my MainWindow with the following call:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
...
I can run your code in python3, but after I exit I get the Segmentation Fault. If I change the constructor to what I indicated above the Fault is not reproduced.
Hope this helps

How do I open sub window after I click on button on main screen in PyQt4

HI I am trying to make a simple converter.
I have used PyQt4 designed to make the Gui
I want to know how launch a new window after I click on the individual button.
This is the interface I have created using PyQt4 Designer.
Here is the Image link :
and I want to launch this windows when I click on currency button.
Here is the Image Link:
Here is my code for main.py
from PyQt4 import QtGui
from main_screen import mainscreen
def main():
import sys
qApp = QtGui.QApplication(sys.argv)
aw = mainscreen()
aw.show()
sys.exit(qApp.exec_())
if __name__ == '__main__':
main()
and code for mainscreen.py
from PyQt4 import QtCore, QtGui
from window_main import Ui_MainWindow
class mainscreen(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(mainscreen,self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
How can I open new window after I click on currency button (object name for currency button is "currency_bt")
and do I have to write the code for currency in same window or I have to write in new window.
How do I do it.
I am new to Python Gui programming.
Each GUI form that you create in Qt Designer needs to be converted into a python module using pyuic. So, to start with, you need to do the same for currency.ui that you did for window_main.
Now you can import your currency window class into mainscreen.py, and connect a button to handler so you can display it.
The code would look something like this:
from PyQt4 import QtCore, QtGui
from window_main import Ui_MainWindow
from currency import Ui_CurrencyWindow
class CurrencyWindow(QtGui.QMainWindow, Ui_CurrencyWindow):
def __init__(self, parent=None):
super(CurrencyWindow, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setupUi(self)
class MainScreen(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainScreen, self).__init__(parent)
self.setupUi(self)
self.currencyButton.clicked.connect(self.handleCurrencyButton)
def handleCurrencyButton(self):
window = CurrencyWindow(self)
window.show()
After looking at this example code, it will probably occur to you that you are going to end up importing a lot of modules, and have a lot of boiler-plate code to write for each one of them (which is not much fun).
So I would advise you to consider changing your GUI design, so that you have one main window containing a tabwidget, and then have a separate tab for each of your converters. This will not only make your application much easier to write, but it should also make it a lot nicer to use.
I'm making my bachelor thesis in PyQt4. First I also wanted to use the designer (generating code is nice), but afterall I was not using it during my work. Maybe it's a matter of taste.
But for your question (I did this without the QtDesigner):
Let's say we have a main window class:
import sys
from PyQt4 import QtCore, QtGui
class mainscreen(QtGui.QMainWindow):
def __init__(self, parent=None):
super(mainscreen,self).__init__(parent)
self.button = QtGui.QPushButton("push")
self.button.clicked.connect(self.pushed)
#pyqtSlot()
def pushed(self):
# in this section here you can create the new window and show it
qApp = QtGui.QApplication(sys.argv)
aw = mainscreen()
aw.show()
sys.exit(qApp.exec_())
There are some good tutorials (http://zetcode.com/gui/pyqt4/ helped me getting started).
Make two programs: main_win.py and second_win.py, then in main_win.py put this lines:
from os import system as sh //In the begin
def openewin(self): //In the class main_win
sh("python second_win.py")
Ready, just connect the push button to function openewin!

Can't get custom slot working in PyQT4 with QT4 designer

I am new to PyQT4.
After some tuts I decided to make a simple GUI in which I will enter text in first line and on clicking of Reverse button ,it will show reversed string on second line.
I made a custom slot for this,by defining the function in my class.But when I click reverse nothing happens. I have used in-bilt slots for Clear button and Exit button in my GUI and they are working perfectly.
If someone can just clarify this custom slot problem it would help me advance further.
Thanks in advance.
Here's a photo of my GUI
http://img196.imageshack.us/img196/7131/diall.png
Stringreverse.py Final File
import sys
from PyQt4 import QtCore, QtGui
from stringreverse_ui import Ui_Dialog
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui=Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton_3,QtCore.SIGNAL("Click()"), self.reverse)
def reverse(self):
s=self.ui.lineEdit_2.text()
self.ui.lineEdit.setText(s[::-1])
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())enter code here
Try using QtCore.SIGNAL("clicked()") instead of QtCore.SIGNAL("Click()").

Categories

Resources