Install Gtk 3.0 problem with depencies in linux

36 Views Asked by At

I am using PyCharm for doing a application in Gtk. First I tried to automatically install the package but it gave me a dependency error and told me to install the Cairo package and something else that I don't remember to be able to correctly install Gtk.

I'm not going to put all the console logs but if it helps I'll paste them or put a link or something that I have saved.

Anyway, after installing these packages it worked for me.

sudo apt-get install libcairo2-dev

sudo apt-get install build-essential

sudo apt-get install python3-dev

sudo apt install libgirepository1.0-dev

To all this, the pycharm packages that I have in the environment are these and they finally stopped giving me errors

enter image description here

My problem is when I try to compile a simple code like this it gives me an error and I am not able to fix it. I don't know if it is a Gtk problem or something I installed, if I am missing something but I can't get it to work.

more info

Error:

home/user/PycharmProjects/tfg/.venv/bin/python /home/user/PycharmProjects/work/prueba.py Traceback (most recent call last): File "/home/user/PycharmProjects/work/prueba.py", line 7, in class MyWindow(Gtk.Window):

AttributeError: type object 'Gtk' has no attribute 'Window' Process finished with exit code 1

import gi

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


class MyWindow(Gtk.Window):
    def __init__(self):
        super().__init__(title="Hello World")

        self.button = Gtk.Button(label="Click Here")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        print("Hello World")

def main():
    win = MyWindow()
    win.connect("destroy", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main() ```
2

There are 2 best solutions below

1
fmi On

Try adding the system libs for GTK3:

sudo apt install libgtk-3-dev

0
Rainor On

I found the problem, if something like this happens to someone and they have all the packages installed and everything correctly updated and they are using pycharm, I will find that the problem may be in the interpreter that you are using in the pycharm environment.

In my case it does not recognize Gtk correctly, I do not know if it is due to some problem with PyGobject or some gcc package that I do not have and it is not able to use the library (this is what I understand is happening to me)

In any case, if you have the packages installed in the system, in my case in Linux and that happens to you with Pycharm, the solution is to use the system interpreter and not the virtual environment, I found it in this link

https://www.reddit.com/r/pycharm/comments/ouuo4d/how_can_i_get_pycharm_to_recognise_gtk_code/