rss_feed

Displaying dialog boxes

homeHome
pagesGTK-3
pagespython
pageswidgets

Dialog Boxes

This example program allows you to open the file selector dialogs and displays the selected files, it also demonstrates selecting colour and font from the built in selector dialogs. once a file font or colour has been selected we grab the resulting value and display it as an example of retrieving the value.

 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
#!/usr/bin/env python
from gi.repository import Gtk


class application_gui:
    """Tutorial 06 demo of file, colour and font dialogs."""

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut06.glade')

        #grab our widget using get_object this is the name of the widget from glade, window1 is the default name
        self.window = xml.get_object('window1')
        self.text = xml.get_object('entry1')

        #load our widgets from the glade file
        self.widgets = {}
        self.widgets['entry'] = xml.get_object('entry2')
        self.widgets['color_button'] = xml.get_object('btnColour')
        self.widgets['font_button'] = xml.get_object('btnFont')
        self.widgets['open_button'] = xml.get_object('btnLoad')
        self.widgets['save_button'] = xml.get_object('btnSave')

        self.widgets['color_button'].connect('clicked', self.colour_selector)
        self.widgets['font_button'].connect('clicked', self.font_selector)
        self.widgets['save_button'].connect('clicked', self.save_file_selector)
        self.widgets['open_button'].connect('clicked', self.open_file_selector)
        
        self.widgets['open_dialog'] = xml.get_object('fileChooserLoad')
        self.widgets['open_dialog'].add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CLOSE)
        self.widgets['open_dialog'].add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
        
        self.widgets['save_dialog'] = xml.get_object('fileChooserSave')
        self.widgets['save_dialog'].add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CLOSE)
        self.widgets['save_dialog'].add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.OK)

        self.widgets['color_dialog'] = xml.get_object('colorSelect')

        self.widgets['font_dialog'] = xml.get_object('fontSelect')

        #connect to events, in this instance just quit our application
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        #show the window else there is nothing to see :)
        self.window.show()

    def colour_selector(self, widget):
        self.widgets['color_dialog'].run()
        self.colour = self.widgets['color_dialog'].get_color_selection().get_current_color()
        
        #accessing the individual values as integers, converting to floats here
        colour = [self.colour.red / 65535.0,
                  self.colour.green / 65535.0,
                  self.colour.blue / 65535.0]

        self.widgets['color_dialog'].hide()
        self.text.set_text(self.widgets['color_dialog'].get_name() + ' ' + self.colour.to_string())

    def font_selector(self, widget):
        self.widgets['font_dialog'].run()
        self.font = self.widgets['font_dialog'].get_font_selection()
        self.text.set_text(self.widgets['font_dialog'].get_name() + ' ' + self.font.get_font_name() + ' ' + str(self.font.get_size()))
        self.widgets['font_dialog'].hide()

    def open_file_selector(self, widget):
        response = self.widgets['open_dialog'].run()
        if self.widgets['open_dialog'].get_filenames():
            self.text.set_text(self.widgets['open_dialog'].get_name() + ' '+ str(self.widgets['open_dialog'].get_filenames()))
        else:
            if self.widgets['open_dialog'].get_filename():
                self.text.set_text(self.widgets['open_dialog'].get_name() + ' '+ self.widgets['open_dialog'].get_filename())
        self.widgets['open_dialog'].hide()

    def save_file_selector(self, widget):
        response = self.widgets['save_dialog'].run()
        self.text.set_text(self.widgets['save_dialog'].get_name() + ' '+ self.widgets['save_dialog'].get_filename())
        self.widgets['save_dialog'].hide()


application = application_gui()
Gtk.main()