0

I am trying to call a specific constructor of the parent class from a subclass. I tried using the method described here, but I am getting an TypeError message.

Here is the test code:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class ComboBoxEntry(Gtk.ComboBox):

    @classmethod
    def new_with_entry(cls):
        super(ComboBoxEntry, cls).new_with_entry()


combo1 = Gtk.ComboBox().new_with_entry()
print(combo1.get_has_entry)
combo2 = ComboBoxEntry().new_with_entry()
print(combo2.get_has_entry)

Gtk.ComboBox has several constructors. Instancing it without specifying any will create the ComboBox instance without Gtk.Entry present. Instancing it with new_with_entry() constructor create an empty ComboBox with Gtk.Entry present.

My subclass is expecting the Gtk.Entry to be present. Thus, I need to somehow invoke the new_with_entry() constructor.

Running the above code produces following Error:

TypeError: ComboBox constructor cannot be used to create instances od a subclass ComboBoxEntry

How can I call a parent class custom constructor?

Shpenat
  • 11
  • 2
  • 1
    Is your subclass expanding on `Gtk.ComboBox` or on `Gtk.Entry`? – mkrieger1 Jan 21 '23 at 01:23
  • Actually both. I use `self.connect('key-press-event, Foo)` to expand on Gtk.ComboBox followed by `child=Gtk.ComboBox.get_child(self)` to connect to the Gtk.Entry. Then I call `child.connect('activate', Bar)`. Unfortunately, without `new_with_entry()` the `child` object is not Gtk.Entry but of type Gtk.CellView, which does not have 'activate' signal. – Shpenat Jan 21 '23 at 11:17
  • But in your code `ComboBoxEntry` is just a subclass of `gtk.ComboBox`, not of `gtk.Entry`. I may not fully understand what you mean by "expand". – mkrieger1 Jan 21 '23 at 11:37
  • I think it is me, who does not understand it correctly. I am pretty new to python ( coming from C bsckground). I came to the conclusion, that I am expanding on the Gtk.ComboBox class and not on Gtk.Entry. My class is subclass of Gtk.Combobox only. – Shpenat Jan 21 '23 at 18:11
  • @Shpenat exactly, and that's why I am confused by your sentence "my subclass is expanding on the Gtk.Entry". – mkrieger1 Jan 21 '23 at 20:14
  • I tried to rephrase the question a bit. I hope this makes it a bit more clear. – Shpenat Jan 21 '23 at 22:13

0 Answers0