I hope this question isn't too far-fetched. I'm good with Selenium and I've been working with PyQt4 recently. I want to use them both together with a program I'm currently working on and it'd work out a lot more smoothly if I could embed the controllable browser into a Qt4 frame or widget. Can this be done? And if so, how?
It doesn't have to be done with Selenium, I just want to be able to control the browser or at least show a webpage in a Qt widget or frame.
So after some research into methods other people have used, I figured it out.
The code I used came from a "very simple browser" module I obtained from here
I modified the code to be more customizable for my future self.
Here's my modified version of the code:
from PyQt4 import QtCore, QtGui, QtWebKit
class Browser(QtGui.QMainWindow):
def __init__(self, size=[800,600], frame=None, centralWidget=None, default_url='https://www.google.com', backButton=True, forwardButton=True, topBar=True):
"""
Initialize the browser GUI and connect the events
"""
self.showBackButton = backButton
self.showForwardButton = forwardButton
self.showTopBar = topBar
QtGui.QMainWindow.__init__(self)
self.resize(size[0],size[1])
if (centralWidget == None):
self.centralwidget = QtGui.QWidget(self)
else:
self.centralwidget = centralWidget
self.mainLayout = QtGui.QHBoxLayout(self.centralwidget)
self.mainLayout.setSpacing(0)
self.mainLayout.setMargin(1)
if (frame == None):
self.frame = QtGui.QFrame(self.centralwidget)
else:
self.frame = frame
self.gridLayout = QtGui.QVBoxLayout(self.frame)
self.gridLayout.setMargin(0)
self.gridLayout.setSpacing(0)
self.horizontalLayout = QtGui.QHBoxLayout()
if (self.showTopBar):
self.tb_url = QtGui.QLineEdit(self.frame)
if (self.showBackButton):
self.bt_back = QtGui.QPushButton(self.frame)
if (self.showForwardButton):
self.bt_ahead = QtGui.QPushButton(self.frame)
if (self.showBackButton):
self.bt_back.setIcon(QtGui.QIcon().fromTheme("go-previous"))
if (self.showForwardButton):
self.bt_ahead.setIcon(QtGui.QIcon().fromTheme("go-next"))
if (self.showBackButton):
self.horizontalLayout.addWidget(self.bt_back)
if (self.showForwardButton):
self.horizontalLayout.addWidget(self.bt_ahead)
if (self.showTopBar):
self.horizontalLayout.addWidget(self.tb_url)
self.gridLayout.addLayout(self.horizontalLayout)
self.html = QtWebKit.QWebView()
self.gridLayout.addWidget(self.html)
self.mainLayout.addWidget(self.frame)
#self.setCentralWidget(self.centralwidget) --- Not needed when embedding into a frame
if (self.showTopBar):
self.connect(self.tb_url, QtCore.SIGNAL("returnPressed()"), self.browse)
if (self.showBackButton):
self.connect(self.bt_back, QtCore.SIGNAL("clicked()"), self.html.back)
if (self.showForwardButton):
self.connect(self.bt_ahead, QtCore.SIGNAL("clicked()"), self.html.forward)
self.connect(self.html, QtCore.SIGNAL("urlChanged(const QUrl)"), self.url_changed)
self.default_url = default_url
if (self.showTopBar):
self.tb_url.setText(self.default_url)
self.open(self.default_url)
def browse(self):
"""
Make a web browse on a specific url and show the page on the
Webview widget.
"""
if (self.showTopBar):
url = self.tb_url.text() if self.tb_url.text() else self.default_url
self.html.load(QtCore.QUrl(url))
self.html.show()
else:
pass
def url_changed(self, url):
"""
Triggered when the url is changed
"""
if (self.showTopBar):
self.tb_url.setText(url.toString())
else:
pass
def open(self, url):
self.html.load(QtCore.QUrl(url))
self.html.show()
It could use some work at the moment, but I've tested it out and it's doing exactly what I need it to do. I tested it out with the following chunk of code that runs when the script is executed
if (__name__ == "__main__"):
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.resize(800,600)
myFrame = QtGui.QFrame(window)
myFrame.resize(200,200)
myFrame.move(10,10)
main = Browser(centralWidget=myFrame, default_url='https://www.google.com/', forwardButton=False, backButton=False, topBar=False)
window.show()
sys.exit(app.exec_())
Like I said, it could use work, but it does exactly what I needed it to do. Now I can embed it into a frame (with the size of my choosing) to use within another application.
Regarding my modifications: I made it possible to keep/remove the back button, forward button and top bar (for the URL). But the webbrowser is still controllable using the "open" function.
And if you wanted to open another webpage, it's as simple as the following
main.open('https://your.webpage.here.com')
Related
I have a QTextBrowser() object:
self.PAddressLink = QTextBrowser()
I need to click on a link placed on this QTextBrowser, and it should open a new dialog box.
self.PAddressLink.setHtml("<html><body><a href=#>+Add Permanent Address</a></body></html>")
I can open the new window with the below code anyhow:
self.PAddressLink.anchorClicked.connect(self.AddPAddress) #self.AddPAddress is the method of displaying a dialog box.
But I need to know if I can place the self.AddPAddress in the href and avoid using the below extra statement:
self.PAddressLink.anchorClicked.connect(self.AddPAddress) #self.AddPAddress
Assuming all the methods are defined on the same object (e.g. self), you could set the method names in the href attribute:
self.PAddressLink.setHtml('...')
self.PAddressLink.anchorClicked.connect(self.handleLink)
and then use getattr to call the method:
def handleLink(self, url):
if url.scheme():
# handle normal urls here if necessary...
else:
getattr(self, url.toString())()
Here's a complete demo using both QLabel and QTextBrowser:
from PyQt5 import QtCore, QtGui, QtWidgets
html = """
<p>Url Link</p>
<p>Method Link</p>
"""
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.label = QtWidgets.QLabel(html)
self.browser = QtWidgets.QTextBrowser()
self.browser.setOpenLinks(False)
self.browser.setHtml(html)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.browser)
layout.addWidget(self.label)
self.label.linkActivated.connect(self.handleLink)
self.browser.anchorClicked.connect(self.handleLink)
def handleLink(self, url):
url = QtCore.QUrl(url)
if url.scheme():
# handle real urls
QtGui.QDesktopServices.openUrl(url)
else:
# handle methods
getattr(self, url.toString())()
def myMethod(self):
QtWidgets.QMessageBox.information(self, 'Test', 'Hello World!')
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
app.exec()
Most likely not. Atleast not any easy way. You'd be just reimplementing the signals and slots system most likely.
Just as with buttons, you have to connect the click signal to a slot. That's how it is designed to work.
I am trying to decouple entirely my GUI from my controller class, and for some reason I can't seem to manage to connect my buttons from outside of my GUI class itself.
Here's a small example of what I mean :
import sys
from PySide6 import QtWidgets
class Gui(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Gui, self).__init__(parent)
layout = QtWidgets.QHBoxLayout(self)
self.button = QtWidgets.QPushButton("Do stuff")
layout.addWidget(self.button)
class Controller(object):
def do_stuff(self):
print("something")
def startup(parent):
ctrl = Controller()
gui = Gui(parent)
gui.button.clicked.connect(ctrl.do_stuff)
return gui
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
gui = startup(dialog)
dialog.show()
sys.exit(app.exec_())
I would expect this code, when run, to display a GUI with one push button (which it does), and when pressing the push button, I'd expect the word "something" to get printed. However this doesn't seem to be the case.
I might just be too tired, but I can't find the solution.
What am I missing?
Thanks a lot in advance!
ctrl = None
gui = None
def startup(parent):
global ctrl
global gui
ctrl = Controller()
gui = Gui(parent)
gui.button.clicked.connect(ctrl.do_stuff)
return gui
try this, and it does work. when the variable is in the function, it will be destroyed before the function is finished. the global variable is not a good coding style but is a simple way to figure out your confusion.
I have an application which has a main window, which can have multiple subwindows. I would like to have one set of QActions in the main window that interact with the currently selected window. For example, the application might be a text editor, and clicking file->save should save the text file the user is currently working on. Additionally, some QActions are checkable, so their checked state should reflect the state of the currently active window.
Here is a minimum working example that has the basic functionality I want, but I suspect there is a better way to do it (further discussion below the code).
import sys
import PyQt4.QtGui as QtGui
class DisplayWindow(QtGui.QWidget):
def __init__(self, parent=None, name="Main Window"):
# run the initializer of the class inherited from
super(DisplayWindow, self).__init__()
self.myLayout = QtGui.QFormLayout()
self.FooLabel = QtGui.QLabel(self)
self.FooLabel.setText(name)
self.myLayout.addWidget(self.FooLabel)
self.setLayout(self.myLayout)
self.is_foo = False
def toggle_foo(self):
self.is_foo = not self.is_foo
if self.is_foo:
self.FooLabel.setText('foo')
else:
self.FooLabel.setText('bar')
class WindowActionMain(QtGui.QMainWindow):
def __init__(self):
super(WindowActionMain, self).__init__()
self.fooAction = QtGui.QAction('Foo', self)
self.fooAction.triggered.connect(self.set_foo)
self.fooAction.setCheckable(True)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(self.fooAction)
self.toolbar = self.addToolBar('File')
self.toolbar.addAction(self.fooAction)
self.centralZone = QtGui.QMdiArea()
self.centralZone.subWindowActivated.connect(
self.update_current_window)
self.setCentralWidget(self.centralZone)
self.create_dw("Window 1")
self.create_dw("Window 2")
def create_dw(self, name):
dw = DisplayWindow(name=name)
self.centralZone.addSubWindow(dw)
dw.show()
def update_current_window(self):
""" redirect future actions to affect the newly selected window,
and update checked statuses to reflect state of selected window"""
current_window = self.centralZone.activeSubWindow()
if current_window:
self.current_dw = self.centralZone.activeSubWindow().widget()
self.fooAction.setChecked(self.current_dw.is_foo)
def set_foo(self):
self.current_dw.toggle_foo()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = WindowActionMain()
ex.show()
sys.exit(app.exec_())
My actual version of DisplayWindow could be useful in many different projects, and I want to package it up so that you don't have to add a lot of code to the main window to use it. Therefore, DisplayWindow, all of its functionality and a list of available actions should be in one module, which would be imported in WindowActionMain's module. I should then be able to add more actions for DisplayWindow without changing any code in WindowActionMain. In particular, I don't want to have to write a little function like WindowActionMain.set_foo(self) just to redirect each action to the right place.
Yes, this is possible by handling the QMenu's aboutToShow signal
and considering the QGuiApplication's focusWindow (or however you get that in Qt4).
Example below shows a generic 'Window' menu acting on the frontmost window.
http://doc.qt.io/qt-4.8/qmenu.html#aboutToShow
http://doc.qt.io/qt-5/qguiapplication.html#focusWindow
def on_windowMenu_aboutToShow(self):
self.windowMenu.clear()
self.newWindowAction = QtWidgets.QAction(self)
self.newWindowAction.setShortcut("Ctrl+n")
self.newWindowAction.triggered.connect(self.on_newWindowAction)
self.newWindowAction.setText("New Window")
self.windowMenu.addAction(self.newWindowAction)
self.windowMenu.addSeparator()
playerWindows = [w for w in self.topLevelWindows() if w.type()==QtCore.Qt.Window and w.isVisible()]
for i, w in enumerate(playerWindows):
def action(i,w):
a = QtWidgets.QAction(self)
a.setText("Show Window {num} - {title}".format(num=i+1, title=w.title()))
a.triggered.connect(lambda : w.requestActivate())
a.triggered.connect(lambda : w.raise_())
self.windowMenu.addAction(a)
action(i,w)
self.windowMenu.addSeparator()
self.closeWindowAction = QtWidgets.QAction(self)
self.closeWindowAction.setShortcut("Ctrl+w")
self.closeWindowAction.triggered.connect(lambda : self.focusWindow().close())
self.closeWindowAction.setText("Close")
self.windowMenu.addAction(self.closeWindowAction)
So, I am new to python programming. I have started to implement a UI in pyqt5 and there I have a button and I want to react when the user clicks it.
According to this Link I should simply write btn.clicked.connect(self.buton_pressed) however I get the message "Cannot find reference connect in function". (The surrounding code is at the end of the question)
So I googled a bit and all I found is that it should just work that way. I just don't get why it does not. I found this Stackoverflow question which also describes the old variant of how to do it. That did not work either, after some googeling I found out that it is no longer supported in pyqt5 or in some other package.
The function where I try to connec to the event:
def __add_button(self, text: str, layout: QLayout):
btn = QPushButton(text, self)
layout.addWidget(btn)
btn.clicked.connect(self.button_pressed)
# TODO: fix this.
return btn
The code where the GUI is generated and the function called, in the __init__ function
lblhm = QLabel("Hauptmessung", self)
layout.addWidget(lblhm)
self.__hm_b = self.__add_button("Messung öffnen", layout)
self.__hm_config_b = self.__add_button("Configruation öffnen", layout)
lblzm = QLabel("Zusatzmessung", self)
layout.addWidget(lblzm)
self.__zm_b = self.__add_button("Messung öffnen", layout)
self.__zm_config_b = self.__add_button("Configuration öffnen", layout)
The button_pressed function is not yet implemented, but it is supposed to open a openFile dialog for file selection.
According to this post i could just connect after returning the function, but then i would have to write it 4 times which is not very nice. Isn't the signal bound to the object not to the variable?
Thankful for any help :)
It's hard to understand your problem since you don't provide us with a working example, i.e. a peace of code one can run "as is". Something like this:
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
def __init__(self):
super().__init__()
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
lblhm = QtGui.QLabel("Hauptmessung", self)
layout.addWidget(lblhm)
self.__hm_b = self.__add_button("Messung öffnen", layout)
self.__hm_config_b = self.__add_button("Configruation öffnen", layout)
lblzm = QtGui.QLabel("Zusatzmessung", self)
layout.addWidget(lblzm)
self.__zm_b = self.__add_button("Messung öffnen", layout)
self.__zm_config_b = self.__add_button("Configuration öffnen", layout)
def button_pressed(self):
print('Button pressed')
def __add_button(self, text: str, layout: QtGui.QLayout):
btn = QtGui.QPushButton(text, self)
layout.addWidget(btn)
btn.clicked.connect(self.button_pressed)
return btn
if __name__== '__main__':
import sys
app = QtGui.QApplication(sys.argv)
wnd = MyWindow()
wnd.show()
sys.exit(app.exec_())
There's no problem with this code under PyQt4. Does it work for with PyQt5?
I want to dive in Python by building a simple browser-application. I've mad a minimalistic webkitbrowser with a tutorial and now want to extend the program, but I'm stuck at some tiny problems I cannot solve.
Python 3.3.3
using Glade for the UI
The first step is to simply add a second scrolledWindow in which the developer-tools should load, immediately.
Here is my .ui-file so far, and this is the python-code:
from gi.repository import Gtk, WebKit
UI_FILE = "browser.ui"
class Browser:
"""A simple Webkit-Browser in GTK+"""
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file(UI_FILE)
self.builder.connect_signals(self)
self.back = self.builder.get_object("back")
self.forward = self.builder.get_object("forward")
self.adress = self.builder.get_object("adress")
self.webview = WebKit.WebView()
scrolled_window = self.builder.get_object("scrolledwindow")
scrolled_window.add(self.webview)
self.settings = WebKit.WebSettings()
self.settings.set_property('enable-developer-extras', True)
self.webview.set_settings(self.settings)
self.devtools = WebKit.WebInspector()
scrolled_window_dev = self.builder.get_object("scrolledwindowDev")
scrolled_window_dev.add(self.devtools)
^^^^^
self.webview.connect("title-changed", self.on_title_changed)
self.window = self.builder.get_object("window")
self.window.show_all()
def on_title_changed(self, webview, frame, title):
self.window.set_title(title)
def on_button_clicked(self, button):
if button.get_stock_id() == Gtk.STOCK_GO_FORWARD:
self.webview.go_forward()
elif button.get_stock_id() == Gtk.STOCK_GO_BACK:
self.webview.go_back()
def on_entry_activate(self, widget):
url = widget.get_text()
if not "http://" in url:
url = "http://"+url
self.webview.load_uri(url)
def destroy(self, window):
Gtk.main_quit()
def main():
app = Browser()
Gtk.main()
if __name__ == "__main__":
main()
I get the error
TypeError: argument widget: Expected Gtk.Widget, but got
gi.repository.WebKit.WebInspector
Okay, this is stated in the reference of Webkit, that WebInspector is a GObject and not a GtkWidget. But I don't know what to do now.
So, can I make a GtkWidget from a GObject (if yes - how) or should I attach the dev-tools in a complete different way?
The inspector, as you noted, isn't a widget. It's a web page, so you need to create another webview for it. You do this by getting self.window.props.web_inspector (don't create a new inspector) and connecting to its inspect-web-view signal. Inside that signal handler, you need to create a new webview, add that webview to a window or wherever you want to display it, and return it.
You'll probably also want to handle the show-window, attach-window, detach-window, and close-window signals.
More documentation here: inspect-web-view
Example of running Inspector in separate window. Webkit-gtk.
This gist without many signals connected.
https://gist.github.com/alex-eri/53518825b2a8a50dd1695c69ee5058cc