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?

Thanks


VSCode extension: 
ms-python.python
ms-python.vscode-pylance

$ 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 = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  box1.append(entry)

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

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

  box2 = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
  box2.append(button)
  box2.append(label)

  # big box
  box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
  box.append(box1)
  box.append(box2)

  # window
  window = Gtk.ApplicationWindow(application=app)
  window.set_title("window")
  window.set_default_size(350, 250)
  window.set_child(box)
  window.present()

def on_button_clicked(button, entry, label):
  label.set_text(entry.get_text())

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

Hi!
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:

# https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Box.html#Gtk.Box.new
  new(orientation, spacing)

Thanks

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,
              spacing=6,
              vexpand=True)

The normal constructors don’t take positional arguments.

1 Like

Thanks, that is the way i wanted.

Hi,

box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
             spacing=6,
             vexpand=True)
  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
    Gtk.Box.

  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/gtkbox.c
GtkWidget*
gtk_box_new (GtkOrientation orientation,
             int            spacing)
{
  return g_object_new (GTK_TYPE_BOX,
                       "orientation", orientation,
                       "spacing", spacing,
                       NULL);
}

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.

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,
             spacing=6,
             vexpand=True)

Thanks

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 https://pypi.tuna.tsinghua.edu.cn/simple --upgrade PyGObject PyGObject-stubs

This call works on my machine:

box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
             spacing=6,
             vexpand=True)

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