How do you create a browser in PyQt5? - python

I have a very simple application that is intended to run a web browser in PyQt window.
The code runs without errors the window opens and then 'blinks' briefly and then is blank.
I am running MacOS(10.13.6) Python 3.8 and PyQt5 5.14 and PyQtWebEngine 5.14.
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow,self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("http://google.com"))
self.setCentralWidget(self.browser)
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

Related

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

Maximize/Restore buttons in the title bar are disabled in a QMainWindow - pyqt5

I was just creating my window in the Qt Designer, when I had finished it I noticed that even opening it from a .py file, the maximize and restore buttons in the title bar are disabled, they appear in there but I can't click it. I am using Windows 10 by the way. It appears like this:
enter image description here
I found some solutions using:
self.setWindowFlag(Qt.WindowMaximizeButtonHint) self.setWindowFlag(Qt.WindowMinimizeButtonHint)
but they don't work in my case.
Here's the complete code:
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
import sys
class mainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(mainWindow, self).__init__()
uic.loadUi('mainMenu.ui', self)
self.show()
self.setWindowFlag(Qt.WindowMaximizeButtonHint)
self.setWindowFlag(Qt.WindowMinimizeButtonHint)
app = QtWidgets.QApplication(sys.argv)
window = mainWindow()
app.exec_()

How to get size of widgets

I am developing a PyQt5 application however I am having issues with the heights of the widgets. Below is a simplified version of my issue:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class App(QWidget):
def __init__(self):
super().__init__()
self.showMaximized()
self.setStyleSheet("QWidget {background: blue;}")
print(self.frameGeometry().height())
self.show()
if __name__ == "__main__":
window = QApplication(sys.argv)
app = App()
window.setStyle(QStyleFactory.create("Fusion"))
window.exec_()
Here I create a window and maximise it. Using a tkinter window, it tells me the height maximised is 841, which is the size of my screen, however the PyQt5 application prints the height to be 519. Is this an issue with the self.showMaximized() method, or some other issue.
Resizing is not instantaneous in Qt. What Qt does is take the information from showMaximized to activate the flag of the native window (library that depends on each OS) then after a time T the OS applies that flag and sends it the new geometry. So in your case you have to give it a delay to get the correct information.
import sys
from PyQt5.QtWidgets import QApplication, QStyleFactory, QWidget
from PyQt5.QtCore import QTimer
class App(QWidget):
def __init__(self):
super().__init__()
self.setStyleSheet("QWidget {background: blue;}")
self.showMaximized()
QTimer.singleShot(100, self.calculate)
def calculate(self):
print(self.frameGeometry().height())
if __name__ == "__main__":
window = QApplication(sys.argv)
app = App()
window.setStyle(QStyleFactory.create("Fusion"))
window.exec_()
On the other hand, if your objective is to know the size of the initial screen then you should not use a QWidget for that since it will depend on the time it takes for Qt and the native library to create the native window, instead use the Screen class :
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QGuiApplication
if __name__ == "__main__":
window = QApplication(sys.argv)
print(QGuiApplication.primaryScreen().availableGeometry().height())

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()

Embedding a terminal in PyQt5

So I've been trying to create my own terminal but that has been proven very glitchy and not professional looking.
Then I stumbled across this code which is for PyQt4:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class embterminal(QWidget):
def __init__(self):
QWidget.__init__(self)
self.process = QProcess(self)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
#self.process.start(
#'xterm',['-into', str(self.terminal.winId())])
# Works also with urxvt:
self.process.start(
'urxvt',['-embed', str(self.terminal.winId())])
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embterminal()
main.show()
sys.exit(app.exec_())
Since my application is written in PyQt5, I naturally tried porting that code to PyQt5.
I changed from PyQt4.QtCore import *
from PyQt4.QtGui import * to from PyQt5.QtCore import *
from PyQt5.QtGui import * and added from PyQt5.QtWidgets import *
Then when I ran my code I realized the terminal didn't pop up.
I wonder why does this happen and is there a workaround ?
I also wonder if I can use both PyQt5 and PyQt4 in the same project/file, even.
In PyQt. QWidget.winId() returns a sip.voidptr object, but if you convert it to an integer, it should work. Here's a working example:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.process = QProcess(self)
self.terminal = QWidget(self)
layout = QVBoxLayout(self)
layout.addWidget(self.terminal)
wid = str(int(self.terminal.winId()))
self.process.start('urxvt', ['-embed', wid])
def closeEvent(self, event):
self.process.terminate()
self.process.waitForFinished(1000)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.setGeometry(100, 100, 800, 600)
window.show()
sys.exit(app.exec_())

Categories