I am trying PyGObject with Gtk4, and my sample code works too.
PyGObject and Gtk4 are great and are almost as easy and convenient
as SwiftUI.
But one thing is that I can not get hints and suggestions for PyGObject
functions and parameters in VSCode. I installed the Python extension
and can have hints for Python built-in functions, but not for PyGObject
ones. Do I miss some packages or configurations?
Do I need to do further steps after pip3 install pygobject-stubs?
Will pygobject-stubs automatically make auto-completion, and parameter hints happen for PyGObject function calls? I still can not get it to work.
$ python3 -m pip show PyGObject-stubs
Name: PyGObject-stubs
Version: 1.0.0
If you used virtualenv to install PyGObject-stubs then you need to select virtualenv’s python interpreter in VSCode. Press F1, type “select” and choose “Python: Select interpreter”.
I did not use venv or virtualenv, but I followed getting_started[1] doc and used homebrew. macOS comes with python 3.9.6 under /usr/bin, but homebrew installs python 3.10.8 under /usr/local/bin. I select the python 3.10.8 interpreter following your help and it works.
I think the combination of Gtk and Python is wonderful. PyGObject is great library. It does not have the weird Slot thing from Qt, and does not have the pretty binding thing from SwiftUI.
The hint by VSCode and PyGObject-stubs shows only that it requires some
tuple *args and dict **kwargs. I still do not know the meaning of the
arguments from this.
Quick note: the “new” methods are wrappers for the C convenience constructors. They are not really needed in Python, where you can pass any GObject property as a named argument to the constructor’s normal form:
That’s the Gtk.Box.new() method, which is a wrapper around the C constructor gtk_box_new().
The Gtk.Box() form takes any object property as named argument. This applies to any GObject.
Any writable GObject property, as listed in the API reference, can be passed to the constructor; this applies recursively for any property defined by any ancestor class of the class you’re constructing.