Part 1b

Create a Simple GUI

The Python

The first thing is to import some libraries needed and make the file so it can be an executable.

#!/usr/bin/env python

import gtk
import gladevcp.makepins
from gladevcp.gladebuilder import GladeBuilder
import hal

The next thing is to create the main class needed.

class gui_one(object):

  # This connects with the signal created when the main window is destroyed
  def on_window1_destroy(self, widget, data=None):
    print "quit with cancel"
    gtk.main_quit()

  # This connects with the menu item signal when we press the quit button
  def on_gtk_quit_activate(self, menuitem, data=None):
    print "quit from menu"
    gtk.main_quit()

  # This section is the init section
  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file("gui1.glade")
    self.halcomp = hal.component("gui1")
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.window.show()
    panel = gladevcp.makepins.GladePanel( halcomp, "gui1.glade", self.builder, None)
    self.halcomp.ready()

In the same directory as the gui1.glade file was saved save the following python code as gui1.py.

gui1.py
#!/usr/bin/env python

import gtk
import gladevcp.makepins
from gladevcp.gladebuilder import GladeBuilder
import hal

class gui_one(object):

  def on_window1_destroy(self, widget, data=None):
    print "quit with cancel"
    gtk.main_quit()

  def on_gtk_quit_activate(self, menuitem, data=None):
    print "quit from menu"
    gtk.main_quit()

  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file("gui1.glade")
    halcomp = hal.component("gui01")
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.window.show()
    panel = gladevcp.makepins.GladePanel( halcomp, "gui1.glade", self.builder, None)
    halcomp.ready()

if __name__ == "__main__":
  app = gui_one()
  gtk.main()

To run our example change the properties of gui1.py to execute by right clicking on the file name and select properties.

Start an Axis simulator then run gui1.py in a terminal. It doesn’t look like much but if you click on the E-Stop button you can see it change in the Axis window as well. Just one little thing but such a huge thing it is to communicate to LinuxCNC from our little GUI.

images/python-01-01.png

We can also see some messages from LinuxCNC in our terminal window when we press the E-Stop button.

images/python-01-02.png

Now when you close the Axis screen you can see our GUI closes also.

Next up adding some content to our glade file…