-3

i'm trying through a python script in conjunction with glade, create a button that opens me a file in python so I can edit if I want to make some changes later. Can anyone help me if you please ?

What i did was this:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject as gobject
import pygtk
import gtk


def show_script(button):
   dialog = gtk.FileChooserDialog("Open...", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))

   dialog.set_default_response(gtk.RESPONSE_OK)

   filter = gtk.FilerFilter()
   filter.set_name("All files")
   filter.add_pattern("*")
   dialog.add_filter(filter)

   response = dialog.run()
   if response == gtk.RESPONSE_OK:
      print (dialog.get_filename(), 'selected')
   elif response == gtk.RESPONSE_CANCEL:
      print  ('Closed, you didnt choose any files')
   dialog.destroy()


builder = Gtk.Builder()
builder.add_from_file("Wi_Green_Sheddule_v1.glade")
handlers = {
      "action_show_script": show_script
   }
}

builder.connect_signals(handlers)
window = builder.get_object("window")
window.show_all()

Gtk.main()

The error that my program does when i click the button is:

Traceback (most recent call last):
  File "/home/pi/Downloads/showShedduleWiGreen.py", line 70, in show_script
    dialog = gtk.FileChooserDialog("Open...", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 62, in __getattr__
    raise AttributeError(_static_binding_error)
AttributeError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183
jcoppens
  • 5,306
  • 6
  • 27
  • 47
Ricardo Alves
  • 145
  • 1
  • 2
  • 8

1 Answers1

1

For starters, you are mixing up Python2 and Python3, and modules from introspection and older non-introspection modules:

from gi.repository import Gtk
from gi.repository import GObject as gobject
import pygtk
import gtk

You are importing Gtk and gtk, which are not mixable. You also don't use GObject in your code, so don't import it.

Leave just

from gi.repository import Gtk

and change all 'gtk's in your code to Gtk.

Then, take care of the indenting - else you'll still have errors. And I couldn't test anymore, as the glade file is not included...

jcoppens
  • 5,306
  • 6
  • 27
  • 47