#+TITLE: Displaying dialog boxes #+DATE: 2014-02-03 12:00:00 UTC #+DESCRIPTION: Demonstration of opening various dialog boxes and retrieving the selected values. #+FILETAGS: GTK-3:python:widgets #+CATEGORY: python #+SLUG: 06-dialog-boxes #+THUMBNAIL:../../../images/gtk/tut06-dialogs.png #+BEGIN_COMMENT .. title: Displaying dialog boxes .. slug: 06-dialog-boxes .. date: 2014-02-03 12:00:00 UTC .. tags: GTK-3, python, widgets .. category: python .. description: Demonstration of opening various dialog boxes and retrieving the selected values. .. type: text #+END_COMMENT #+CAPTION: Dialog Boxes [[../../../images/gtk/tut06-dialogs.png]] 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. #+BEGIN_SRC python #!/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() #+END_SRC