Demo entry 6339254

Glades.py

   

Submitted by Michael Leidel on Dec 20, 2016 at 22:17
Language: Python 3. Code size: 1.9 kB.

# prints out the code for connecting glade 
# signals to python handlers
# argument 1 is the glade file name
#
# Execute with 'gladly.sh' /usr/local/bin/
#
import sys
import fileinput


if len(sys.argv) < 2:
    print("Missing File Argv 1")
    exit()

print ('''
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

class Mainclass: 

  # may need to change 'window1' to your id for GtkWindow
  def on_window1_destroy(self, object, data=None): Gtk.main_quit()
  #def on_btn_close_clicked(self, object, data=None): Gtk.main_quit()

  def __init__(self):
    self.gladefile = "''' + sys.argv[1] + '''"
    self.builder = Gtk.Builder()
    self.builder.add_from_file(self.gladefile)
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.window.show()
''')

for line in fileinput.input(sys.argv[1]):
    if " id=" in line:
        text = line.split()[2]
        currid = text.split('"')[1]
    if "handler=" in line and "GtkWindow" not in line:
        text = line.split()[2]
        text = text.split('"')[1]
        print("  def " + text + "(self, widget):")
        if currid in text:
            print ('    obj = self.builder.get_object("' + currid + '")\n')
    if "class name=" in line:
        text = line.split('"')[1]
        print("  # Style Class: " + text + "\n")

print('''
if __name__ == "__main__":
  main = Mainclass()
  Gtk.main()
''')

#######################################################################
# Gather all of the "id" names and print commented for reference

idarr = []

for line in fileinput.input(sys.argv[1]):
    if " id=" in line:
      text = line.split()[2]
      idarr.append(text.split('"')[1]) 

print("# LIST OF WIDGET IDS")
print("'''")
c = 0
for item in idarr:
  print( item + " ", end="" )
  c += 1
  if c > 4:
    c = 0
    print()

print()
print("'''")

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).