from PyQt5 import Qt
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import *
from sys import argv
class Window(QMainWindow):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl('https://www.duckduckgo.com'))
self.browser.urlChanged.connect(self.update_AddressBar)
self.setCentralWidget(self.browser)
self.navigation_bar = QToolBar('Navigation Toolbar')
self.addToolBar(self.navigation_bar)
self.navigation_bar.setAttribute(Qt.Qt.WA_StyledBackground, True)
self.navigation_bar.setStyleSheet('background-color: white;')
self.navigation_bar.setMinimumSize(0, 75)
self.navigation_bar.setMovable(False)
back_button = QAction("←", self)
back_button.setStatusTip('Go to previous page you visited')
back_button.triggered.connect(self.browser.back)
self.navigation_bar.addAction(back_button)
next_button = QAction("→", self)
next_button.setStatusTip('Go to next page')
next_button.triggered.connect(self.browser.forward)
self.navigation_bar.addAction(next_button)
refresh_button = QAction("⟳", self)
refresh_button.setStatusTip('Refresh this page')
refresh_button.triggered.connect(self.browser.reload)
self.navigation_bar.addAction(refresh_button)
home_button = QAction("⌂", self)
home_button.setStatusTip('Go to home page (Google page)')
home_button.triggered.connect(self.go_to_home)
self.navigation_bar.addAction(home_button)
#self.navigation_bar.addSeparator()
self.URLBar = QLineEdit()
self.URLBar.returnPressed.connect(lambda: self.go_to_URL(QUrl(self.URLBar.text()))) # This specifies what to do when enter is pressed in the Entry field
self.navigation_bar.addWidget(self.URLBar)
self.addToolBarBreak()
self.show()
def go_to_home(self):
self.browser.setUrl(QUrl('https://www.duckduckgo.com/'))
def go_to_URL(self, url: QUrl):
if url.scheme() == '':
url.setScheme('http://')
self.browser.setUrl(url)
self.update_AddressBar(url)
def update_AddressBar(self, url):
self.URLBar.setText(url.toString())
self.URLBar.setCursorPosition(0)
app = QApplication(argv)
app.setApplicationName('Libertatem Browser')
window = Window()
app.exec_()
As you can see in my code, I have 4 QActions. I want to make these bigger. How can I do that? I have seen others using back_button.geometry()
, but you can't do that with QActions.
I would prefer to keep them as QActions, because it took me a long while to set them up. As a side note, would changing the font size make the button bigger?
CodePudding user response:
If you change the size of the button associated with the QAction it would not make a big difference, you could do it in the following way:
button = toolbar.widgetForAction(action)
button.setFixedSize(100, 100)
But in this case it is better to change the font size since the size of the button (and the height of the QToolBar) depends on it.
import sys
from PyQt5.QtCore import Qt, QUrl, QSize
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QAction, QApplication, QLineEdit, QMainWindow, QToolBar
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Window(QMainWindow):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("https://www.duckduckgo.com"))
self.browser.urlChanged.connect(self.update_AddressBar)
self.setCentralWidget(self.browser)
self.navigation_bar = QToolBar("Navigation Toolbar", movable=False)
self.addToolBar(self.navigation_bar)
self.navigation_bar.setAttribute(Qt.WA_StyledBackground, True)
self.navigation_bar.setStyleSheet("background-color: white;")
font = QFont()
font.setPixelSize(40)
back_action = QAction("←", self)
back_action.setStatusTip("Go to previous page you visited")
back_action.triggered.connect(self.browser.back)
back_action.setFont(font)
self.navigation_bar.addAction(back_action)
next_action = QAction("→", self)
next_action.setStatusTip("Go to next page")
next_action.triggered.connect(self.browser.forward)
next_action.setFont(font)
self.navigation_bar.addAction(next_action)
refresh_action = QAction("⟳", self)
refresh_action.setStatusTip("Refresh this page")
refresh_action.triggered.connect(self.browser.reload)
refresh_action.setFont(font)
self.navigation_bar.addAction(refresh_action)
home_action = QAction("⌂", self)
home_action.setStatusTip("Go to home page (Google page)")
home_action.setFont(font)
home_action.triggered.connect(self.go_to_home)
self.navigation_bar.addAction(home_action)
self.URLBar = QLineEdit()
self.URLBar.returnPressed.connect(self.handle_return_pressed)
self.navigation_bar.addWidget(self.URLBar)
def go_to_home(self):
self.browser.setUrl(QUrl("https://www.duckduckgo.com/"))
def go_to_URL(self, url: QUrl):
if url.scheme() == "":
url.setScheme("http://")
self.browser.setUrl(url)
self.update_AddressBar(url)
def update_AddressBar(self, url):
self.URLBar.setText(url.toString())
self.URLBar.setCursorPosition(0)
def handle_return_pressed(self):
url = QUrl.fromUserInput(self.URLBar.text())
self.go_to_URL(url)
def main():
app = QApplication(sys.argv)
app.setApplicationName("Libertatem Browser")
window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()