putting glade interface in python

后端 未结 5 656
别跟我提以往
别跟我提以往 2021-02-06 02:30

I\'ve made a gui in glade that I want to put in a python program. I was adapting the instructions from a tutorial I found online to load in my glade file (http://www.pygtk.org/a

5条回答
  •  自闭症患者
    2021-02-06 03:01

    Since I always end up having problems with this, here is a Python 2.7 code that I use for one or the other:

    for Libglade:

    # needs libglade (not for gtk-builder)
    import pygtk
    pygtk.require("2.0")
    import gtk
    import gtk.glade
    
    gladefile = "test-libglade.glade"
    wTree = gtk.glade.XML(gladefile)
    window = wTree.get_widget("MainWindow")
    if (window):
      window.connect("destroy", gtk.main_quit)
    
    window.show_all() # must have!
    gtk.main()
    

    For GtkBuilder:

    # needs gtk-builder (not for libglade)
    import pygtk
    pygtk.require("2.0")
    import gtk
    
    gladefile = "test-gtkbuilder.glade"
    wTree = gtk.Builder()
    wTree.add_from_file(gladefile)
    window = wTree.get_object("MainWindow")
    if (window):
      window.connect("destroy", gtk.main_quit)
    
    window.show_all() # must have!
    gtk.main()
    

    In Glade, you can just add a Window, call it MainWindow, and save two versions with the respective filenames as above for each format; and these snippets should work with them respeactively.

    Hope this helps someone,
    Cheers!

提交回复
热议问题