« Graduate Recruitment
A Health and Safety Perspective of Cycling Safety »

Skipping a gtk.Assistant Page Using set_forward_page_func()

A.K.A. Skipping Page 3!

I regularly use the excellent rednotebook for my daily notes at work. I recently decided I wanted to add a feature to the export dialogue which would allow the user to export only a selection of text.   A code reviewer suggested that, if the user has selected this new export option on one page, the next page is not relevant and should be skipped. Easier said than done.

The export dialogue makes use of gtk.Assistant. Having looked at the documentation, I found def set_forward_page_func(page_func, data) and got my hopes up. Unfortunately, I was unable to find any examples of this code in action. After much trial and error, I found that the page_func needs to accept page (the index of the current page) and needs to return the index of the next page to go to.

I found an example of gtk.Assistant from pygtk-tutorial on gitorious.org and have edited it here to include the ability to skip page 3. I hope this helps someone!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

import gtk

class Assistant:
  def __init__(self):
    assistant = gtk.Assistant()

    assistant.connect("apply", self.button_pressed, "Apply")
    assistant.connect("cancel", self.button_pressed, "Cancel")

    vbox = gtk.VBox()
    vbox.set_border_width(5)
    page = assistant.append_page(vbox)
    assistant.set_page_title(vbox, "Page 1: Starting Out")
    assistant.set_page_type(vbox, gtk.ASSISTANT_PAGE_INTRO)
    label = gtk.Label("""This is an example label within an Assistant. The
              Assistant is used to guide a user through configuration of an
              application.""")
    label.set_line_wrap(True)
    vbox.pack_start(label, True, True, 0)
    assistant.set_page_complete(vbox, True)

    vbox = gtk.VBox()
    vbox.set_border_width(5)
    assistant.append_page(vbox)
    assistant.set_page_title(vbox, "Page 2: Moving On...")
    assistant.set_page_type(vbox, gtk.ASSISTANT_PAGE_CONTENT)
    label = gtk.Label("""This is an example of a label and checkbox within a
          page. If this box is checked, the assistant will skip page 3.""")
    label.set_line_wrap(True)
    self.checkbutton = gtk.CheckButton("Skip next page")
    vbox.pack_start(label, True, True, 0)
    vbox.pack_start(self.checkbutton, False, False, 0)
    assistant.set_page_complete(vbox, True)

    vbox = gtk.VBox()
    vbox.set_border_width(5)
    assistant.append_page(vbox)
    assistant.set_page_title(vbox, "Page 3: The skipped page")
    assistant.set_page_type(vbox, gtk.ASSISTANT_PAGE_CONTENT)
    label = gtk.Label("""This is an example of a page that contains
                information that may be skipped depending on the status of
                the previous pages.""")
    label.set_line_wrap(True)
    button = gtk.Button("Button Example")
    vbox.pack_start(label, True, True, 0)
    vbox.pack_start(button, False, False, 0)
    assistant.set_page_complete(vbox, True)

    vbox = gtk.VBox()
    vbox.set_border_width(5)
    assistant.append_page(vbox)
    assistant.set_page_title(vbox, "Page 4: The Finale")
    assistant.set_page_type(vbox, gtk.ASSISTANT_PAGE_CONFIRM)
    label = gtk.Label("""This is the final page of the Assistant widget. It
                         could be used to confirm the preferences we have
                         set in the previous pages.""")
    label.set_line_wrap(True)
    vbox.pack_start(label, True, True, 0)
    assistant.set_page_complete(vbox, True)

    assistant.show_all()
    # Set an alternative function to return the next page to go to
    assistant.set_forward_page_func(self.pageforward)

  def pageforward(self,page):
    """
    Function called when the forward button is pressed,
    Arguments:
    page:
    integer index of the current page
    returns:
    integer index of the next page to display
    """
    if page == 1 and self.checkbutton.get_active():
      return 3
    else:
      return page+1

  def button_pressed(self, assistant, button):
    print "%s button pressed" % button
    gtk.main_quit()

Assistant()
gtk.main()
Go Top
Previous:
« Graduate Recruitment
Next:
A Health and Safety Perspective of Cycling Safety »

Comments

I would love to know what you think. To comment on this article, send me an email

No comments yet.