No PyGObject functions and parameters hints or suggestions in VSCode

Hello community,

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?


VSCode extension: 

$ apt list gir1.2-gtk-4.0 libcairo2-dev libgirepository1.0-dev \
    python3 python3-gi python3-gi-cairo python3.10-dev 

gir1.2-gtk-4.0 4.6.6 [installed]
libcairo2-dev 1.16.0 [installed]
libgirepository1.0-dev 1.72.0-1 [installed]
python3-gi-cairo 3.42.1 [installed]
python3-gi 3.42.1 [installed]
python3.10-dev 3.10.6-1~22.04.1 [installed]
python3 3.10.6-1~22.04 [installed]

$ python3 -m pip show pycairo pygobject
Name: pycairo
Version: 1.21.0
Name: PyGObject
Version: 3.42.2

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

def on_activate(app):
  # box1
  entry = Gtk.Entry()
  box1 =, 0)

  # box2
  label = Gtk.Label(label='label')

  button = Gtk.Button(label='button')
  button.connect('clicked', on_button_clicked, entry, label)

  box2 =, 0)

  # big box
  box =, 0)

  # window
  window = Gtk.ApplicationWindow(application=app)
  window.set_default_size(350, 250)

def on_button_clicked(button, entry, label):

app = Gtk.Application(application_id='example.GtkApplication')
app.connect('activate', on_activate)

You can use pygobject-stubs.
See also [1], [2]

1 Like

Thanks Yuri,

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”.

Standard intellisence should work:

1 Like

Hi Yuri, It works! Thank you for the kind help.

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.

[1] Getting Started — PyGObject

Hi, Yuri,

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.

# ../gi-stubs/repository/Gtk.pyi
  new(*args, **kwargs)

Is there a way, by VSCode and PyGObject-stubs, shows the argument names?
Like the nice API document:

  new(orientation, spacing)


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:

box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,

The normal constructors don’t take positional arguments.

1 Like

Thanks, that is the way i wanted.


box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
  1. the Gtk.Box constructor takes two args, except for self, how can it
    take the third vexpand arg? There are not default args or dict args for

  2. How can I figure out what else args can a function take - a function
    like Gtk.Box?

# ../python3.10/site-packages/gi-stubs/repository/Gtk.pyi
class Box(Orientable, Container):

  def __init__(self, 
              orientation: Optional[Orientation] = None, 
              spacing: Optional[int] = None) -> None: ...
gtk_box_new (GtkOrientation orientation,
             int            spacing)
  return g_object_new (GTK_TYPE_BOX,
                       "orientation", orientation,
                       "spacing", spacing,

That’s the 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.

1 Like

Hi ebassi,

# ../python3.10/site-packages/gi-stubs/repository/Gtk.pyi
class Box(Orientable, Container):
    def __init__(self, 
                orientation: Optional[Orientation] = None, 
                spacing: Optional[int] = None) -> None: ...

    def new(*args, **kwargs): ...

Will not the __init__ be called for following code? The __init__ has
only two args except for self?

It calls new first, then __init__ right? How does all this work?

box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,


I have no idea what that class definition is from. It’s definitely not inside pygobject.

If it’s from the pygobject code assistance, then it’s wrong and you should not use it.

Hi ebassi,

# ../python3.10/site-packages/gi-stubs/repository/Gtk.pyi

class Box(Orientable, Container):
    container = ...

    def __init__(self, orientation: Optional[Orientation] = None, spacing: Optional[int] = None) -> None: ...
    def get_baseline_position(*args, **kwargs): ...
    def get_center_widget(*args, **kwargs): ...
    def get_homogeneous(self) -> bool: ...
    def get_spacing(self) -> int: ...
    def new(*args, **kwargs): ...
    def pack_end(self, child: Widget, expand: bool, fill: bool, padding: int) -> None: ...
    def pack_start(self, child: Widget, expand: bool, fill: bool, padding: int) -> None: ...
    def query_child_packing(*args, **kwargs): ...
    def reorder_child(self, child: Widget, position: int) -> None: ...
    def set_baseline_position(*args, **kwargs): ...
    def set_center_widget(self, widget: Optional[Widget]) -> None: ...
    def set_child_packing(*args, **kwargs): ...
    def set_homogeneous(self, homogeneous: bool) -> None: ...
    def set_spacing(self, spacing: int) -> None: ...

Then it’s wrong, and you should not be using it. Don’t know what to tell you, other than that.

Sorry, I used homebrew to install other packages.

The PyGObject, PyGObject-stubs are installed by pip3 install. But I installed it from a faster mirror site in the country.

$ python3 -m pip install -i --upgrade PyGObject PyGObject-stubs

This call works on my machine:

box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.