Using GTK3 Socket For Embedding Window in Another Window (Python) - python

I want to embed a window in GTK window by using socket. I have found an example, changed it to run by using GTK3, but it did not work. It gives following error:
AttributeError: 'gi.repository.Gtk' object has no attribute 'Socket'
It needs X11 platform but are there any solutions to use it on Windows?
Software Information:
Python: 3.8.6,
GTK3,
Windows 10 (x64).
Code:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
import string
import sys
# Some code here
socket = Gtk.Socket()
socket.show()
window1.add(socket)
print("Socket ID=", socket.get_id())
window1.connect("destroy", Gtk.main_quit)
def plugged_event(widget):
print("I (",widget,") have just had a plug inserted!")
socket.connect("plug-added", plugged_event)
if len(sys.argv) == 2:
socket.add_id(long(sys.argv[1]))
builder.connect_signals(Signals())
window1.show_all()
Gtk.main()

GtkPlug and GtkSocket at X11-only widgets, and do not work on any other windowing system/platform.

Related

GTK4 Window Created From XML Immediately Closes

I create a GTK4 Window from an XML file via Python.
When I run the code, the window actually pops up very briefly (and all controls are there as expected), but then closes immediately. I assume there is something missing in my code, but I can't figure out what it is from the documentation.
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw
class MyApp(Adw.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.connect('activate', self.on_activate)
def on_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("main.glade")
self.win = builder.get_object("window")
self.win.present()
app = MyApp(application_id="com.example.GtkApplication")
app.run(sys.argv)
Fixed it :-)
The solution is a simple one-liner:
self.win.set_application(app)

Detect when a window opens in wnck python

I am making a window tiling script using libwnck. I would like to detect when the user opens a new window in order to resize it. Here is the gist of my code so far:
import gi
gi.require_version("Wnck", "3.0")
from gi.repository import Wnck
screen = Wnck.Screen.get_default()
screen.force_update()
# Here I resize all windows in the current workspace using
# window.set_geometry
# The script ends here however I'd like it to continue forever
# and detect a "window opened event"
From the documentation it looks like there are virtual methods like do_window_opened but I have no idea how to get it working in python.
Here is a working simple test code. Hope it can help you and is what you want.
import gi
gi.require_version('Wnck', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Wnck
from gi.repository import Gtk
def test_do_when_window_opened_simple():
Gtk.init([])
screen: Wnck.Screen = Wnck.Screen.get_default()
screen.force_update()
def do_window_opened(this_screen: Wnck.Screen, opened_window: Wnck.Window):
print('hello')
app: Wnck.Application = opened_window.get_application()
app_name = app.get_name()
print('app name -> ' + app_name)
print('window name -> ' + opened_window.get_name())
screen.connect('window-opened', do_window_opened)
Gtk.main()
if __name__ == '__main__':
test_do_when_window_opened_simple()
See also:
Please check the first comment of this link
Please check the example of this link, it's written in C though
Note: the Wnck.Screen object has a function named 'do_window_opened', but it's not implemented. I received an error message 'gi.repository.GLib.GError: g-invoke-error-quark: Class WnckScreen doesn't implement window_opened (1)' when trying to call it.

Python: WebKit.WebView: how to reload on error?

I recently asked this question, which was answered. I'm trying exactly the same, but with WebKit.WebView with GTK and I'm stuck at the same part.
Goal: Load another URL if the first isn't reachable.
import gi, time
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit', '3.0')
from gi.repository import Gtk, WebKit
browser = WebKit.WebView()
browser.load_uri('http://this-domain-does-not-exist.tld')
def load_error(webview, event, url, error):
webview.load_uri('http://google.com') # not working
browser.connect('load-error', load_error)
win = Gtk.Window()
win.add(browser)
win.show_all()
Gtk.main()
Any idea? Thanks in advance!
For some reason commands that are run within the error callback are ignored. A fix is to add the fallback uri loading after all other events are processed. Like this:
from gi.repository import Gtk, WebKit, GLib
....
def load_error(webview, event, url, error):
GLib.idle_add(webview.load_uri, 'http://google.com')
I got it working by moving to WebKit2 and returning True in the callback. It's possible that only the return works for WebKit as well but I don't have that available to test. Modified code:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2
browser = WebKit2.WebView()
browser.load_uri('http://this-domain-does-not-exist.tld')
def load_failed(webview, event, url, error):
webview.load_uri('http://google.com')
return True
browser.connect('load-failed', load_failed) # Changed from load-error
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.add(browser)
win.show_all()
Gtk.main()
From the documentation:
Returns: True to stop other handlers from being invoked for the event. False to
propagate the event further.

Glade 3 imports not working for both gtk and gi

import gtk
class Buglump:
def on_window1_destroy(self, object, data=None):
print "quit with cancel"
gtk.main_quit()
def on_gtk_quit_activate(self, menuitem, data=None):
print "quit from menu"
gtk.main_quit()
def __init__(self):
self.gladefile = "tutorial-1.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(self.gladefile)
self.builder.connect_signals(self)
self.window = self.builder.get_object("window1")
self.window.show()
if __name__ == "__main__":
main = Buglump()
gtk.main()
So I am using this source code to attempt to use the GUI builder glade, however I keep running into many different errors and am doubting if I am even taking the right approach. From my understanding, you generate this code in a different python shell and it will produce whatever you have built in glade. However I keep running into errors, the current one being :
ModuleNotFoundError: No module named 'gtk'
I am seeking guidance of how to move forward using glade, I am very new to python so I apologize if this is not a good question. I can not find any way to use this program from anywhere I have looked online.
In an app in development, I have following below; perhaps you can use something similar.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk, Gdk as gdk, GLib, GObject as gobject

Stop Python Gtk.Application using Ctrl+C

I am creating an application. Previously, I were using Gtk.Main() to start my application, and created some hooks to stop the application from the command line using Ctrl+C. Now, I have migrated the application to a more "standard" Gtk.Application, but can't get it to stop using Ctrl+C.
This is a very simple Gtk.Application, that when is run from the command line, it can't be stopped using Ctrl+C:
from gi.repository import Gtk
import sys
# a Gtk ApplicationWindow
class MyWindow(Gtk.ApplicationWindow):
# constructor: the title is "Welcome to GNOME" and the window belongs
# to the application app
def __init__(self, app):
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
class MyApplication(Gtk.Application):
# constructor of the Gtk Application
def __init__(self):
Gtk.Application.__init__(self)
# create and activate a MyWindow, with self (the MyApplication) as
# application the window belongs to.
# Note that the function in C activate() becomes do_activate() in Python
def do_activate(self):
win = MyWindow(self)
# show the window and all its content
# this line could go in the constructor of MyWindow as well
win.show_all()
# start up the application
# Note that the function in C startup() becomes do_startup() in Python
def do_startup(self):
Gtk.Application.do_startup(self)
# create and run the application, exit with the value returned by
# running the program
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
import signal
from gi.repository import GLib
...
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, app.quit)
This worked with Gtk 3.0:
import signal
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
app = Application()
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
You can use Ctrl+Z to stop the execution of script.

Categories