使用按钮调用带有glade的file.py

时间:2017-06-26 11:37:08

标签: python-3.x pygtk glade pygobject

我正在尝试使用python脚本和glade,创建一个按钮,在python中打开一个文件,以便我可以编辑,如果我想稍后进行一些更改。如果你愿意,有人可以帮助我吗?

我做的是这个:

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()

单击按钮时我的程序执行的错误是:

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

1 个答案:

答案 0 :(得分:1)

对于初学者来说,你正在混合Python2和Python3,以及来自内省和旧的非内省模块的模块:

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

您正在导入Gtk和gtk,它们不可混合。你也没有在你的代码中使用GObject,所以不要导入它。

离开

from gi.repository import Gtk

并将代码中的所有>更改为Gtk。

然后,照顾缩进 - 否则你仍然会有错误。我不能再测试了,因为没有包括林间空地文件......