Thoughts on Technology

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 1 February 2013

Tutorial 2: ELM Images, File Selector and Popups

Posted on 14:22 by Unknown
This is the second post in my series on developing GUI applications in Elementary using Python. Today we are going to continue building on the Hello Elementary example I started in the first tutorial. In today's post I will only be covering the code that is different from our previous examples, so if you haven't looked that one over yet please take a moment to do so now.

You can find the full source code for all of today's examples here.

Example 3:
We are going to start off by displaying a static, pre-defined image in our GUI:


It only takes us 8 lines of actual code to create and display the above image in our program:

    #Creates an Image object that displays an image
ic = elementary.Image(window)

#Use the os module to get the current path to our .py file. Our image is relative to our .py We do this because it is best to use the absolute file path to images for the best results.
location = os.path.dirname(os.path.abspath(__file__))

#Tell our icon to auto-fill open space
ic.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
ic.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Here we set the image we want our icon to display
ic.file_set("%s/images/logo.png"%location)

#Optional, lets add mouse over text to our image
ic.tooltip_text_set("Look a pretty picture!")

#Lets show our icon
ic.show()

box.pack_end(windytax)
#Pack our icon between our text and button
box.pack_end(ic)
box.pack_end(button)

In this example we utilize the elementary Image object to display our selected .png file.

Example 4:
Very rarely do we want to simply display a single image for as long as our program is running. So lets give the user the ability to change the image we display in our program:


Elementary has a built in FileselectorButton object that when clicked presents our user with a nice file selector GUI:


The new code to add this file selector button looks like:

    #Creates a "FileselectorButton" object. This is a button (just like we have created before) except that when it is click it automatically opens a file selector window
fsb = elementary.FileselectorButton(window)

#We can set the text of our fsb just like a normal button text
fsb.text = "Change Image"

#Tooltip for mouse over
fsb.tooltip_text_set("Click Me!")

#This tells our file selector window what to do when our user selects a file. The first argument is the callback function we want run and our second argument is our image object we want to change the display of
fsb.callback_file_chosen_add(change_image, ic)

#Show our button
fsb.show()

box.pack_end(windytax)
box.pack_end(ic)
#Pack our file selector button between our image and button
box.pack_end(fsb)
box.pack_end(button)

window.resize_object_add(box)

window.resize(300,300)

window.show()

#Our fileselector callback. The file argument is the fileselectbutton object. The second argument is the full path to the file that was selected. The final argument is the image object we passed to this callback
def change_image(fsb, file_selected, image):
#Check to make sure a file of some sort was selected. If nothing was selected file_selected will equal None type
if file_selected:
#These are the extensions we will allow our program to display
validExtensions = [".png", ".jpg", ".gif"]

#Use the os module to easily get the extension of our file
fileName, fileExtension = os.path.splitext(file_selected)

#If the extension is in our validExtenions lets check the image we are displaying!
if fileExtension in validExtensions:
image.file_set(file_selected)

Example 5:
Lets add one finishing touch to our application. If our user selects a file to display that doesn't have a valid image extension lets send them a popup telling them why the image displayed wasn't changed:


Showing a popup of this nature is fairly easy using elementary's Popup object. So the final edit to our code looks like this:

#This time we also pass the window object to our change image function. The reason for this is that our popup object needs a parent window object
def change_image(fsb, file_selected, image, window):
if file_selected:
validExtensions = [".png", ".jpg", ".gif"]

fileName, fileExtension = os.path.splitext(file_selected)

if fileExtension in validExtensions:
image.file_set(file_selected)
else:
#if we have an invalid extension lets give the user a popup message telling them why the image didn't change

#Create a popup message
popup = elementary.Popup(window)

#Set the title of our popup
popup.part_text_set("title,text", "Invalid File Extension")

#Set the text of our popup
popup.text = "File %s has an invalid file extension of %s"%(fileName, fileExtension)

#Create a button object
bt = elementary.Button(window)

#Set it's text
bt.text = "OK"

#Define a callback that is called when the button is clicked, lets pass our popup object to this call back so we can close the popup when the user presses OK
bt.callback_clicked_add(bnt_close, popup)

#Sets content for our popup. The first argument is an arbitrary name for the content piece and the second argument is the elementary object you would like displayed for the content
popup.part_content_set("button1", bt)

#Show the popup to our user
popup.show()

#The callback for our popup's OK button. The first agurment is the button object itself and the second object is the popup we passed to it
def bnt_close(bt, popup):
#Lets delete the popup so it goes away
popup.delete()

Hope everyone learned something today! Have any questions feel free to drop a comment below or start a discussion on our user boards.

Resources for this Lesson:
  • Displaying Images
  • Image
  • FileselectorButton
  • Popup 
~Jeff Hoogland
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in elementary, python, tutorial | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Pinguy OS - Distro Review
    My favourite type of distros are Ubuntu based. For some time now I have been making a case for why you should be using Linux Mint. Even thou...
  • HOWTO: Watch Netflix on Bodhi Linux
    Not being able to utilize the Netflix video streaming service has been an issue on the Linux desktop for the past few years. This is due to...
  • Mugen Power Extended Battery N900 - Hands on Review
    A few months back I wrote a post mentioning that I had ordered a Mugen Power battery for my N900 . If you have been by my blog here before...
  • HOWTO: Ubuntu Linux on T101MT
    I wrote a HOWTO for getting Linux working on the T91MT a couple months back and as I mentioned here I ended up changing to the slightly la...
  • Blackboard - No Linux for Online Education
    In addition to being a Linux Advocate and working 40+ hours a week I am also a full time student. Due to schedule constraints I often take ...
  • HOWTO: Test E18, EFL 1.8.0, and Terminology 0.4.0 on Bodhi Linux
    As of this past weekend the testing builds of the Enlightenment window manager DR18 (E18 for short) are in the Bodhi Linux testing repositor...
  • Wine vs Native - 3D Performance Benchmarks
    In the past I've done Wine on Linux versus native Windows 7 benchmarks for 3D applications . Source engine games are some of my favorite...
  • Seven Things to do after Installing Bodhi Linux
    So you've taken the plunge and opted to install Bodhi Linux. Perhaps you read a recent review or one of the screen shots in our gallery ...
  • Bigger is Better... Right?
    It appears the mantra of "bigger is better" has gripped developers of the late as the handsets we see keep getting larger and larg...
  • HOWTO: Bodhi Linux on Genesi Smartbook
    Edit/Update: You can find the latest release here ->  http://www.bodhilinux.com/downloads_mobile.php I mentioned a short while ago that G...

Categories

  • 3g modem
  • adobe
  • android
  • appeal
  • apple
  • arm
  • art
  • asus tablet
  • benchmark
  • bodhi
  • bordeaux
  • cedega
  • chakra
  • chrome os
  • chromebook
  • cockatrice
  • codeweavers
  • comic
  • cricket wireless
  • crysis
  • cxgames
  • debian
  • dell duo
  • diablo3
  • distro review
  • dtf
  • e18
  • eandora
  • eccess
  • elementary
  • elive
  • enlightenment
  • fedora
  • firefox
  • gaming
  • genesi
  • gnome
  • google
  • google chrome
  • google wave
  • handheld device
  • hardware
  • helios
  • howto
  • html5
  • ideapad
  • interview
  • ipad
  • jolicloud
  • kde
  • l4d2
  • laptops
  • lenovo
  • linux
  • lxde
  • macbook
  • math
  • maxima
  • media
  • meego
  • milestone
  • mint
  • mir
  • mk802
  • moblin
  • n900
  • netflix
  • nexus 7
  • nvidia
  • open pandora
  • open source
  • opengl
  • opera
  • operating systems
  • palm
  • phones
  • promotion
  • python
  • qt
  • rant
  • raspberry pi
  • reviews
  • sabayon
  • software
  • source games
  • spotlight
  • sprint
  • starcraft2
  • steam
  • t-mobile
  • tutorial
  • ubuntu
  • unigine
  • unity
  • wayland
  • web application
  • windows
  • windows 7
  • wine
  • wxmaxima
  • xfce

Blog Archive

  • ▼  2013 (20)
    • ►  December (1)
    • ►  November (1)
    • ►  September (1)
    • ►  June (1)
    • ►  May (2)
    • ►  March (2)
    • ▼  February (3)
      • A Fat Stack of Bodhi Linux
      • Comparison of Linux Desktops OpenGL Performance
      • Tutorial 2: ELM Images, File Selector and Popups
    • ►  January (9)
  • ►  2012 (57)
    • ►  December (5)
    • ►  November (4)
    • ►  October (2)
    • ►  September (1)
    • ►  August (4)
    • ►  July (9)
    • ►  June (4)
    • ►  May (4)
    • ►  April (1)
    • ►  March (7)
    • ►  February (6)
    • ►  January (10)
  • ►  2011 (107)
    • ►  December (8)
    • ►  November (8)
    • ►  October (5)
    • ►  September (14)
    • ►  August (9)
    • ►  July (8)
    • ►  June (7)
    • ►  May (10)
    • ►  April (9)
    • ►  March (13)
    • ►  February (9)
    • ►  January (7)
  • ►  2010 (122)
    • ►  December (10)
    • ►  November (8)
    • ►  October (10)
    • ►  September (14)
    • ►  August (17)
    • ►  July (10)
    • ►  June (9)
    • ►  May (14)
    • ►  April (8)
    • ►  March (7)
    • ►  February (7)
    • ►  January (8)
  • ►  2009 (27)
    • ►  December (10)
    • ►  November (7)
    • ►  October (10)
Powered by Blogger.

About Me

Unknown
View my complete profile