Have you ever been in a situation where you wanted to read/inspect some value that GTK Inspector does not show or modify something in your running app e.g. replace a widget with another one? Well! I have just the trick for you (if your app is written in an interpreted language that is).
The Trick
The trick is to start/run the app in a background thread of the interpreter while the main thread is showing a prompt for you to type and run more code in.
Steps
- Make sure the file/module that contains
Application.run()
call can be imported in live interpreter mode (running the interpreter directly) - Replace the
Application.run()
call withGio.Task.run_in_thread()
call passing a wrapper aroundApplication.run
astask_func
argument. - Run the interpreter.
- Import the file/module that contains
Gio.Task.run_in_thread()
call. - If the
Gio.Task.run_in_thread()
call is inside another function, call that function. Otherwise, do nothing.
The app will start running but you’ll still be able to input more code into the interpreter prompt and read/modify any part of your app while it is running.
Example (Python)
-
Save the following code as
bg_app.py
.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import gi gi.require_version("Gtk", "4.0") from gi.repository import Gtk, Gio task = Gio.Task() app = Gtk.Application() win = Gtk.ApplicationWindow() def on_activate(app): app.add_window(win) win.present() def run_app(*args): app.run() app.connect('activate', on_activate) task.run_in_thread(run_app)
-
Open the Python interpreter in the same directory where you saved
bg_app.py
. -
Enter the following line.
1
from bg_app import app, win, Gtk
This will launch the app without blocking the interpreter.
-
Enter the following line and see the magic.
1
win.props.child = Gtk.Label.new("Wow, Magic!")