Deprecated: Function split() is deprecated in /home/www/static/linuxsoft.ro/www.linuxsoft.ro/public_html/wiki/inc/auth.php on line 146 Warning: Cannot modify header information - headers already sent by (output started at /home/www/static/linuxsoft.ro/www.linuxsoft.ro/public_html/wiki/inc/auth.php:146) in /home/www/static/linuxsoft.ro/www.linuxsoft.ro/public_html/wiki/inc/auth.php on line 236 Warning: Cannot modify header information - headers already sent by (output started at /home/www/static/linuxsoft.ro/www.linuxsoft.ro/public_html/wiki/inc/auth.php:146) in /home/www/static/linuxsoft.ro/www.linuxsoft.ro/public_html/wiki/inc/actions.php on line 128 programare:exemple:gnome-druids-python [Linux Soft Wiki]
 

I Put A Spell On You: Gnome Druids

Possible romanian version Fara Vraja: Gnome Druids
Cristian L. Vlasceanu

Un tutorial despre crearea vrajitorilor, meniurilor de instalare folosind widgeturile Gnome UI Druids.
Link la pagina de manual care descrie API-ul C pentru Gnome Druids 2.2. Cine are un link la pagina de manual Python sa posteze aici.

Post pe forum sursa acestei pagini.

Purpose of the article

Describe the gnome.ui.Druid, gnome.ui.DruidPageEdge and gnome.ui.DruidPageStandard components.
By the end of the article, the reader should be able to write her own Druid scripts using the Python programming language and the gtk module.

Article Layout

Introduce Druids by comparison to MS Windows Wizards.
Wizard-like applications are typically written to guide the user through some sort of setup or installation task. They shield the user from the details of shell commands and shell scripts.
Gnome Druids are the equivalent of Windows’ Wizards.

Benefits: improve usability, make the system / application look professional, and more appealing to novice users.
Introduce the components.

Present small example. Code samples will be introduced gradually.

A practical application: a druid to guide the user through a simple backup procedure: selected files will be archived to CDROM (will reuse the Gnome nautilus-cd-burner components).
Create the ISO disc image using the Mkisofs utility.
Outline the architecture of the druid application: object-oriented, each page is derived off a gnome.ui.DruidPage, etc...

The Code so Far

Posted on LinuxSoft forum on 2006/1/8.

You will also need the OpenClipArt images from this archive to be able to test the code.

The following listing of the source code can be also downloaded here.

#! /usr/bin/env python
# vim: expandtab
 
# Copyright (c) 2007 Cristian L. Vlasceanu
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
# AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
 
import gnome.ui
import gtk
import os # for popen
 
title_text = "Quick CDROM Backup"
intro_text = "This wizard will guide you through the process of\
 backing up your files onto a CD.\n\n\
 Enjoy the cool and free clipart from http://www.openclipart.org/"
 
final_text = "You are now ready to burn the CD.\n\
Click the Apply button to proceed."
 
 
class PageEvents(object):
	"""
	Helper class, handles common signal events from druid pages
	""" 
	def __init__(self, druidPage):
		druidPage.connect("cancel", self.__on_cancel)
		#clipart courtesy of the Open Clipart project
		pixbuf = gtk.gdk.pixbuf_new_from_file(
				"slim_cd_drive_frederic_m_01.png")
		druidPage.set_logo(pixbuf)
 
 
	def __on_cancel(self, page, druid):
		druid.quit()
 
 
class Wizard(gnome.ui.Druid):
	"""
	A design refinement: move the quit confirmation dialog
	here, so that we can invoke it when the user clicks the
	X button on the toplevel window decoration
	"""
	def __init__(self, app):
		gnome.ui.Druid.__init__(self)
		self.__app = app
 
	def quit(self):
		dlg = gtk.MessageDialog(None,	#parent
								0,		#flags
								gtk.MESSAGE_QUESTION, 
								gtk.BUTTONS_YES_NO,
								"Do you really want to cancel?")
		dlg.connect("response", self.__on_response)
		dlg.set_transient_for(self.get_toplevel())
		dlg.run()
	
	def __on_response(self, dialog, response):
		dialog.hide()
		if response == gtk.RESPONSE_YES:
			gtk.main_quit()
 
	def get_file_entries(self):
		return self.__app.get_file_entries()
 
	def get_volume_id(self):
		return self.__app.get_volume_id()
 
 
class FileEntry(gtk.Frame):
	"""
	A custom widget for entering a file name
	"""
	def __init__(self, label = None):
		gtk.Frame.__init__(self, label)
		hbox = gtk.HBox()
		hbox.set_border_width(5)
		self.add(hbox)
		self.__entry = gtk.Entry()
		hbox.pack_start(self.__entry)
		btn = gtk.Button("Browse...")
		btn.connect("clicked", self.__on_browse)
		hbox.pack_end(btn, False)
 
	def __on_browse(self, btn):
		#use FileSelection rather than FileChooser,
		#for backwards compatibility with Pygtk 2.0
		fs = gtk.FileSelection()
		fs.hide_fileop_buttons()
		fs.set_transient_for(self.get_toplevel())
		fs.connect("response", self.__on_response)
		fs.run()
 
	def __on_response(self, dialog, response):
		dialog.hide()
		if response == gtk.RESPONSE_OK:
			filename = dialog.get_filename()
			if filename:
				self.__entry.set_text(filename)
 
	def get_text(self):
		return self.__entry.get_text()
 
 
class FirstPage(gnome.ui.DruidPageEdge):
	"""
	First druid page, displays an introductory message
	outlining the purpose of this application
	"""
	def __init__(self):
		"""
		Constructor, initialize the page as a start page
		"""
		gnome.ui.DruidPageEdge.__init__(self, gnome.ui.EDGE_START)
		self.__events = PageEvents(self)
		self.set_title(title_text)
		self.set_text(intro_text)
 
		pixbuf = gtk.gdk.pixbuf_new_from_file("folder_cd.png")
		self.set_watermark(pixbuf)
		self.show_all()
 
 
class FinalPage(gnome.ui.DruidPageEdge):
	def __init__(self):
		gnome.ui.DruidPageEdge.__init__(self, gnome.ui.EDGE_FINISH)
		self.__events = PageEvents(self)
		self.set_text(final_text)
		self.set_title("Congratulations")
		self.connect("finish", self.__on_apply)
		pixbuf = gtk.gdk.pixbuf_new_from_file("cdwriter_mount.png")
		self.set_watermark(pixbuf)
		self.show_all()
 
	def __on_apply(self, page, druid):
		druid.set_show_finish(False)
		volid = druid.get_volume_id()
		if not volid:
			volid = "BACKUP"
		files = ""
		for e in druid.get_file_entries():
			fileName = e.get_text()
			if fileName:
				files += " " + '"' + fileName + '"'
		if not files:
			#todo: the previous page should gray out the
			# Forward button if no files are selected.
			dlg = gtk.MessageDialog(druid.get_toplevel(),
									gtk.DIALOG_MODAL,	
									gtk.MESSAGE_ERROR, 
									gtk.BUTTONS_CLOSE,
									"No files selected")
			dlg.connect("response", lambda dlg, response: dlg.destroy())
			dlg.run()
			return
		command = 'mkisofs -V "' + volid + '" -rock_ridge -o tmp.iso' 
		command += files
		p = os.popen(command, 'r')
		status = p.close()
		if status:
			pass #todo: display an error message dialog
		else:
			p = os.popen("nautilus-cd-burner --source-iso=tmp.iso && rm tmp.iso")
			if not p.close():
				gtk.main_quit() #we're done!
 
 
#This class used to be called MainPage -- which is too generic,
#does not convey the real purpose which is to collect the file names.
#It also implies that it is the ONLY standard page, thus imposing
#an arbitrary limit on the design.
#So let's fix it and call it something more suggestive:
class FileNameEntryPage(gnome.ui.DruidPageStandard):
	def __init__(self):
		gnome.ui.DruidPageStandard.__init__(self)
		self.__events = PageEvents(self)
		self.set_title("Select Files")
		vbox = gtk.VBox()
		self.vbox.add(vbox)
		self.__entry = []
		for i in range(0, 6):
			self.__entry.append(FileEntry("Add File #" + str(i + 1)))
			vbox.pack_start(self.__entry[i], False)
		self.show_all()
 
	def get_file_entries(self):
		return self.__entry
 
 
class VolumeOptionsPage(gnome.ui.DruidPageStandard):
	def __init__(self):
		gnome.ui.DruidPageStandard.__init__(self)
		self.__events = PageEvents(self)
		self.set_title("Volume Options")
		frame = gtk.Frame("Volume ID")
		frame.set_border_width(5)
		self.vbox.pack_start(frame, False)
		hbox = gtk.HBox()
		hbox.set_border_width(5)
		frame.add(hbox)
		self.__entry = gtk.Entry()
		hbox.add(self.__entry)
 
	def get_volume_id(self):
		return self.__entry.get_text()
 
 
class WizardApp(gtk.Window):
	"""
	The main window of our application
	"""
	def __init__(self):
		gtk.Window.__init__(self, "toplevel")
		self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
		self.set_title("Example CDROM Backup")
		self.set_size_request(500, 480)
		self.connect("delete_event", self.__on_delete)
		self.__firstPage = FirstPage()
		self.__finalPage = FinalPage()
		self.__optionsPage = VolumeOptionsPage()
		self.__filesPage = FileNameEntryPage()
		self.__druid = Wizard(self)
		self.__druid.append_page(self.__firstPage)
		self.__druid.append_page(self.__optionsPage)
		self.__druid.append_page(self.__filesPage)
		self.__druid.append_page(self.__finalPage)
		self.add(self.__druid)
 
	def __on_delete(self, app, event):
		self.__druid.quit()
		return True
 
	def get_file_entries(self):
		return self.__filesPage.get_file_entries()
 
	def get_volume_id(self):
		return self.__optionsPage.get_volume_id()
 
 
if __name__ == "__main__":
	app = WizardApp()
	app.show_all()
	gtk.main()

Conclusion

Writing a Druid application with Python is fun and not very difficult; system administrators and Linux distribution vendors may write Druid applications to entice more users to the world of Linux and Gnome.

 
programare/exemple/gnome-druids-python.txt · Last modified: 2008/03/02 17:38 by pghoratiu
 
Publicaţi pe acest Wiki doar conţinut original, nu se acceptă copierea articolelor de pe alte site-uri. Se recomandă folosirea link-urilor in acest caz.
Recent changes RSS feed