Stop loop in import - python

I am making a custom console/shell window module using pyqt5 I have basically everything working but when I import it the whole entire script freezes because of a loop in the module that I import.
This is the module that gets imported:
import sys
import time
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QRunnable, QThreadPool
from PyQt5.QtWidgets import QTextEdit
class SpamWorker(QRunnable):
def __init__(self, console: QTextEdit):
super().__init__()
self.console = console
def run(self):
while True:
time.sleep(0.2)
self.console.append('SPAM!')
class Printer(QtWidgets.QMainWindow, QRunnable):
def __init__(self):
super(Printer, self).__init__()
uic.loadUi('window.ui', self)
self.setWindowTitle("Console+")
self.thread_pool = QThreadPool()
self.thread_pool.start(SpamWorker(self.Console))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Printer()
window.show()
sys.exit(app.exec_())
how can I import it without it freezing?

Related

Showing output from two different classes in pyqt5 gui python

Program
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import *
from PySide2 import *
from ui_test import *
class MainThread(QThread):
def __init__(self):
super(MainThread,self).__init__()
def run(self):
self.task()
def task(self):
while True:
com="runnning"
print(com)
startexecution = MainThread()
class main(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ti = Ui_MainWindow()
self.ti.setupUi(self)
self.ti.run_btn.clicked.connect(self.starttask)
self.ti.close.clicked.connect(self.close)
self.show()
def starttask(self):
startexecution.start()
if __name__=="__main__":
app =QApplication(sys.argv)
window = main()
sys.exit(app.exec_())
It is an example of the problem I am facing in my original program
MainThread class run is the main program that runs in the backend and the main class runs GUI in frontend. I want to show the output generated by the backend program in textbrowser of my UI as an output

Switching ui without new window PyQT5

I want to change the UI without opening new windows. The problem is that when I do this on widgets then the app is not frameless. When I do it without widgets I have no idea how to change the UI.
Without widget
import sys, time, os
from PyQt5 import uic
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
import sqlite3
class LoginMenu(QWidget):
def __init__(self):
super(LoginMenu, self).__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
uic.loadUi("LoginMenu.ui",self)
self.clicked = False
def mousePressEvent(self, event):
self.old_pos = event.screenPos()
def mouseMoveEvent(self, event):
if self.clicked:
dx = self.old_pos.x() - event.screenPos().x()
dy = self.old_pos.y() - event.screenPos().y()
self.move(self.pos().x() - dx, self.pos().y() - dy)
self.old_pos = event.screenPos()
self.clicked = True
return QWidget.mouseMoveEvent(self, event)
app = QApplication(sys.argv)
log = LoginMenu()
log.show()
sys.exit(app.exec())
with widgets to run app
import sys, time, os
from PyQt5 import uic
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
class LogMenu(QWidget):
def __init__(self):
super(LogMenu, self).__init__()
uic.loadUi("LoginMenu.ui",self)
self.SignUp.clicked.connect(self.gotoReg)
def gotoReg(self):
reg=Register()
widget.addWidget(reg)
widget.setCurrentIndex(widget.currentIndex()+1)
class Register(QWidget):
def __init__(self):
super(Register, self).__init__()
uic.loadUi("SignUp.ui",self)
#main
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
log=LogMenu()
reg=Register()
widget.addWidget(log)
widget.addWidget(reg)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting")
sry for my eng if sth i wrote sth bad
tbh idk how to make it frameless with widgets and draggable with clicking and holding the app or switchable without using widget to run it

QtWebPageRenderer SIGILL issue

My problem is summed in title. When I call method setHtml on instance of QtWebPageRenderer, SIGILL signal is emitted and my application goes down.
I'm aware that this issue is caused by bad Qt5 dynamic library but I installed it with:
sudo pip install PyQt5 --only-binary PyQt5
sudo pip install PyQtWebEngine --only-binary PyQtWebEngine
so I thought I will get correct precompiled library. When I tried to install PyQt5 without --only-binary, I always ended with some strange compilation error. Something like qmake is not in PATH even though it is and I'm able to call qmake from shell.
So my question is, how to make PyQt5 running on Fedora 31 without any SIGILLs.
EDIT:
Following code can replicate the issue. That information about SIGILL is little inaccurate because first signal is actually SIGTRAP, after I hit continue in gdb, I got SIGILL. This hints that Qt is actually trying to say something to me, although in not very intuitive way.
After some playing around with it, I found that without thread, its ok. Does this mean that Qt forces user to use QThread and not python threads? Or it means that I can't call methods of Qt objects outside of thread where event loop is running?
import signal
import sys
import threading
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class WebView(QWebEnginePage):
def __init__(self):
QWebEnginePage.__init__(self)
self.loadFinished.connect(self.on_load_finish)
def print_result(self, data):
print("-" * 30)
print(data)
with open("temp.html", "wb") as hndl:
hndl.write(data.encode("utf-8"))
def on_load_finish(self):
self.toHtml(self.print_result)
class Runner(threading.Thread):
def __init__(self, web_view):
self.web_view = web_view
threading.Thread.__init__(self)
self.daemon = True
def run(self):
self.web_view.load(QtCore.QUrl("https://www.worldometers.info/coronavirus/"))
def main():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtWidgets.QApplication(sys.argv)
web_view = WebView()
runner = Runner(web_view)
runner.start()
app.exec_()
if __name__ == "__main__":
main()
You have to have several restrictions:
A QObject is not thread-safe so when creating "web_view" in the main thread then it is not safe to modify it in the secondary thread
Since the QWebEnginePage tasks run asynchronously then you need a Qt eventloop.
So if you want to use python's Thread class then you must implement both conditions:
import signal
import sys
import threading
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class WebView(QWebEnginePage):
def __init__(self):
QWebEnginePage.__init__(self)
self.loadFinished.connect(self.on_load_finish)
def print_result(self, data):
print("-" * 30)
print(data)
with open("temp.html", "wb") as hndl:
hndl.write(data.encode("utf-8"))
def on_load_finish(self):
self.toHtml(self.print_result)
class Runner(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.daemon = True
def run(self):
# The QWebEnginePage was created in a new thread and
# that thread has an eventloop
loop = QtCore.QEventLoop()
web_view = WebView()
web_view.load(QtCore.QUrl("https://www.worldometers.info/coronavirus/"))
loop.exec_()
def main():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtWidgets.QApplication(sys.argv)
runner = Runner()
runner.start()
app.exec_()
if __name__ == "__main__":
main()
In reality QThread and threading.Thread() are native thread handlers of the OS, so in practical terms it can be said that QThread is a threading.Thread() + QObject with an eventloop running on the secondary thread.
On the other hand, if your objective is to call a function from a thread to which it does not belong, then you should use asynchronous methods as pointed out in this answer.
In this case the simplest is to use pyqtSlot + QMetaObject:
import signal
import sys
import threading
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class WebView(QWebEnginePage):
def __init__(self):
QWebEnginePage.__init__(self)
self.loadFinished.connect(self.on_load_finish)
def print_result(self, data):
print("-" * 30)
print(data)
with open("temp.html", "wb") as hndl:
hndl.write(data.encode("utf-8"))
def on_load_finish(self):
self.toHtml(self.print_result)
#QtCore.pyqtSlot(QtCore.QUrl)
def load(self, url):
QWebEnginePage.load(self, url)
class Runner(threading.Thread):
def __init__(self, web_view):
self.web_view = web_view
threading.Thread.__init__(self)
self.daemon = True
def run(self):
url = QtCore.QUrl("https://www.worldometers.info/coronavirus/")
QtCore.QMetaObject.invokeMethod(
self.web_view,
"load",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(QtCore.QUrl, url),
)
def main():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtWidgets.QApplication(sys.argv)
web_view = WebView()
runner = Runner(web_view)
runner.start()
app.exec_()
if __name__ == "__main__":
main()
Or functools.partial() + QTimer
from functools import partial
import signal
import sys
import threading
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWebEngineWidgets import QWebEnginePage
class WebView(QWebEnginePage):
def __init__(self):
QWebEnginePage.__init__(self)
self.loadFinished.connect(self.on_load_finish)
def print_result(self, data):
print("-" * 30)
print(data)
with open("temp.html", "wb") as hndl:
hndl.write(data.encode("utf-8"))
def on_load_finish(self):
self.toHtml(self.print_result)
class Runner(threading.Thread):
def __init__(self, web_view):
self.web_view = web_view
threading.Thread.__init__(self)
self.daemon = True
def run(self):
wrapper = partial(
self.web_view.load,
QtCore.QUrl("https://www.worldometers.info/coronavirus/"),
)
QtCore.QTimer.singleShot(0, wrapper)
def main():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtWidgets.QApplication(sys.argv)
web_view = WebView()
runner = Runner(web_view)
runner.start()
app.exec_()
if __name__ == "__main__":
main()

view.showMaximised() not working in PyQt5

I am making a web browser using PyQt5. I am using the following code:
import PyQt5
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
import sys
from optparse import OptionParser
class Browser(QWebView):
def __init__(self):
# QWebView
self.view = QWebView.__init__(self)
#self.view.setPage(MyBrowser())
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
#super(Browser).connect(self.ui.webView,QtCore.SIGNAL("titleChanged (const QString&)"), self.adjustTitle)
def load(self,url):
self.setUrl(QUrl(url))
def adjustTitle(self):
self.setWindowTitle(self.title())
app = QApplication(sys.argv)
view = Browser()
view.showMaximized()
view.load("https://duckduckgo.com")
app.exec_()
However, this is what I get:
Can someone please tell me where I am going wrong? Note that it is not a problem of the website. I have tried it with Wikipedia, Stack Overflow and Google. I am using PyQt5 version 5.10.1.
In case you want fullscreen, you have to use:
class Browser(QWebView):
def __init__(self):
# QWebView
self.view = QWebView.__init__(self)
#self.view.setPage(MyBrowser())
self.setWindowTitle('Loading...')
self.titleChanged.connect(self.adjustTitle)
self.showFullScreen()
class Browser(QWebView):
def __init__(self):
super().__init__()
def load(self, url):
self.setUrl(QUrl(url))
def adjustTitle(self):
self.setWindowTitle(self.title())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Browser()
window.setWindowTitle('Loading...')
window.titleChanged.connect(window.adjustTitle)
window.load("https://duckduckgo.com")
window.showMaximized()
sys.exit(app.exec_())
The program does not know the real sizes of your device so it creates a maximum on its assumed geometry.
You should provide the actual geometry by resize then call showMaximized. So that your geometry will be reachable by the program and a true maximized window will be displayed.
self.resize(998, 878)
self.showMaximized()

itemClicked in QlistWidget, function executed more than 1 time

I have a strange problem, hope someone can clear it for me
import os
from os import path
import sys
import pathlib
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget,
QWizard, QWizardPage, QLineEdit, \
QTabWidget, QApplication,
QTextEdit,QToolTip,QPushButton,QMessageBox
from PyQt5.QtCore import QSize,pyqtSlot,pyqtProperty
from PyQt5.QtGui import QFont
from PyQt5.uic import loadUiType
app = QApplication(sys.argv)
if getattr(sys, 'frozen', False):
# we are running in a bundle
installPath = sys._MEIPASS
print('we are running in a bundle')
else:
# we are running in a normal Python environment
installPath = os.path.dirname(os.path.abspath(__file__))
print('we are running in a normal Python environment')
UI_File, _ = loadUiType(path.join(path.dirname(__file__), 'test.ui'))
class MainAPP(QTabWidget, UI_File):
def __init__(self, parent=None):
super(MainAPP, self).__init__(parent)
self.setupUi(self)
self.handle_buttons()
def handle_buttons(self):
self.pushButton.clicked.connect(self.test_2)
def test_2(self):
for i in range(10):
self.listWidget.addItem(str('lklk'))
self.listWidget.itemClicked.connect(self.test)
def test(self):
for i in range(10):
self.listWidget_2.addItem(str('DDD'))
self.listWidget_2.itemClicked.connect(self.test_3)
def test_3(self):
print ('hi')
def main():
app = QApplication(sys.argv)
main = MainAPP()
main.show()
app.exec_()
if __name__ == "__main__":
main()
so basically, I have a push button, if I click on it it will display some data at listWidget and if I clicked on any item in listWidget , it will display other data on ListWidget_2 and then if I click on item in List_widget_2 it then should print ('Hi')
the problem is if I click multiple times in ListWidget and then click on an item in ListWidget_2 , I received more than one ('Hi) , it will diplay ('Hi') according to the number of clicks I clicked in the Listwidget
any idea what could be the issue
You only need to make a connection between a signal and a slot once. Currently you are making additional connections each time you click an item in the first list widget, which results in your method printing "hi" executing once for every connection you made.
To fix this, make both of the signal connections either in the test_2 method or in the __init__ method

Categories