Python Webkit-Browser: Problems on attaching the developer-toolbar - python

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

Related

PyGObject: How to create hamburger menu programmatically

I would like to create a "primary menu" programmatically, which I believe is also called a "hamburger menu". I have done several of these while working on web development side, but I have never done these using Python and GTK. This topic seems to be controversial and there are a lot of different solutions out there. I would like to create a menu like this using the non-deprecated way.
In the documentation is mentioned that the old style menus are deprecated (the whole section is archived under "deprecated"): https://python-gtk-3-tutorial.readthedocs.io/en/latest/menus.html
In this example, the whole HeaderBar is made programmatically and a (popover) menu is added into it:
GtkMenuButton popups
While that seems to do the trick, it's not a "hamburger" menu and the documentation seems to suggest "Your menus should be defined in XML using Gio.Menu":
https://python-gtk-3-tutorial.readthedocs.io/en/latest/application.html#menus
So I am quite lost here. Can someone give me an example how to achieve this? Preferably done programmatically, but if the XML is the only way then so be it.
Thanks in advance!
I don't have enough reputation to comment so I'll just put this here: you could use a GMenu: https://wiki.gnome.org/HowDoI/GMenu
I post my solution that I was able to do with the kind help from irc.gnome.org #python channel. It's not perfect, menu actions are still not working, but at least I got the menu done which was the point of this post.
#!/usr/bin/env python3
# Python imports
import sys
# GTK imports
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gio
from gi.repository import Gtk
class AppWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_border_width(10)
self.set_default_size(640, 480)
open_selection = Gtk.ModelButton(action_name="open_file", label="Open")
about_selection = Gtk.ModelButton(action_name="about_application", label="About")
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, margin=10, spacing=10)
vbox.add(open_selection)
vbox.add(about_selection)
vbox.show_all()
self.popover = Gtk.Popover()
self.popover.add(vbox)
self.popover.set_position(Gtk.PositionType.BOTTOM)
menu_button = Gtk.MenuButton(popover=self.popover)
menu_icon = Gtk.Image.new_from_icon_name("open-menu-symbolic", Gtk.IconSize.MENU)
menu_icon.show()
menu_button.add(menu_icon)
menu_button.show()
headerbar = Gtk.HeaderBar()
headerbar.props.show_close_button = True
headerbar.props.title = "Hamburger Menu Demo"
headerbar.add(menu_button)
headerbar.show()
self.set_titlebar(headerbar)
def open_file(self, widget):
print("Open file")
class Application(Gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, application_id="org.example.myapp")
self.window = None
def do_startup(self):
Gtk.Application.do_startup(self)
action = Gio.SimpleAction.new("open_file", None)
action.connect("activate", self.open_file)
action.set_enabled(True)
self.add_action(action)
action = Gio.SimpleAction.new("about_application", None)
action.connect("activate", self.on_about)
self.add_action(action)
action = Gio.SimpleAction.new("quit", None)
action.connect("activate", self.on_quit)
self.add_action(action)
def do_activate(self):
# We only allow a single window and raise any existing ones
if not self.window:
# Windows are associated with the application
# when the last one is closed the application shuts down
self.window = AppWindow(application=self, title="Main Window")
self.window.present()
def open_file(self, action, param):
print("Open file")
def on_about(self, action, param):
print("About application")
def on_quit(self, action, param):
self.quit()
if __name__ == "__main__":
app = Application()
app.run(sys.argv)

How to embed Selenium Firefox browser into PyQt4 frame

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

In PyQt, how does one get a shared menu and toolbar to talk to the currently active subwindow?

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)

Gtkbuilder connect signals organization

How can i organize the handlers of my application so that i would not have only one class for my application? To be more precise my application is currently organized in the following way:
class main_application(object):
def handler1():
...
def handler2():
...
#lots of handlers...
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("gui.ui")
self.builder.connect_signals(self)
#build window
#....
#tab 1
#tab 2
#and etc..
self.builder.connect_signals(self)
self.gtk_main_window.show_all()
if __name__ == '__main__':
main_application()
gtk.main()
So my problem is that i want to make make a class for every tab(GtkNotebook)/handler i have for my application but i have a problem when calling self.builder.connect_signals(self), it only connects the signals for my main window and not for the rest of the application.
I found solution for my problem, instead of using GtkBuilder i used different format of the glade file (libglade) and replaced the connect_signals with signal_autoconnect:
def __init__(self):
self.gladefile = "gui.glade"
self.builder = gtk.glade.XML(self.gladefile, "gtk_main_window")
self.gtk_main_window = self.builder.get_widget("gtk_main_window")
self.builder.signal_autoconnect(self)

My signals are being ignored!

So I developed a UI in Glade and am coding the program in Python. For some reason, all of my signals are being ignored! Though I've connected them correctly (I think), clicking on the buttons does absolutely nothing!
Below is the code that I'm using to load the ui and connect the signals. Can anyone see WHY they might be being ignored?
class mySampleClass(object):
def __init__(self):
self.uiFile = "MainWindow.glade"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.uiFile)
self.window = self.wTree.get_object("winMain")
if self.window:
self.window.connect("destroy", gtk.main_quit)
dic = { "on_btnExit_clicked" : self.clickButton, "on_winMain_destroy" : gtk.main_quit }
self.wTree.connect_signals(dic)
self.window.show()
else:
print "Could not load window"
sys.exit(1)
def clickButton(self, widget):
print "You clicked exit!"
def exit(self, widget):
gtk.main_quit()
def update_file_selection(self, widget, data=None):
selected_filename = FileChooser.get_filename()
print selected_filename
if __name__ == "__main__":
MyApp = MySampleClass()
gtk.main()
I'm not completely sure that this is the answer, but I know that a number of PyGTK objects cannot send signals themselves - gtk.Image and gtk.Label are two such examples. The solution is to place the widget inside of an event box (gtk.EventBox) and link the events to that.
I do not know if this is the case with tree objects, however. All the same, worth investigating, imho.

Categories

Resources