Jeff Hoog Land

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

Tuesday, 29 January 2013

Tutorial 1: Hello Elementary

Posted on 17:03 by Unknown
This post is the first in a series I am going to be publishing about using elementary and python to develop applications. The source code for all of the examples I am providing can be found in a GitHub repository here. Looking to get help with development? - We have started a programming focused section of the Bodhi Linux forums here. Other great resources for getting help are the Enlightenment devel mailing list as well as #e on freenode IRC. I've also added the python elementary API documentation to the Bodhi website here.

Example 1:
Since most people (myself included) learn best through examples, let's dive right into the code. To start, we are going to be creating a simple window that displays some text to us. It will look something like this:


Including my comments explaining what each line of code does, it takes us less than 50 lines of code to get the above window on our screen. Let's take a look (you can also find the source code for this lesson here):

#Import the elementary library so we can use it
import elementary

#Import evas, used for resizing things
import evas

#A function that creates and shows an elementary window
def hello_elementary():
#Creates a "Standard" elementary window. The first argument is the name of our window. The second argument is the title displayed on the window bar
window = elementary.StandardWindow("hello world", "Hello Elementary")

#callback_delete_request_add tells our window what to do when it's "close" button is pressed
window.callback_delete_request_add(lambda o: elementary.exit())

#Content for our window. Creates a "Label" object which display text in our window. Whenever we create an elementary object we must provide a parent window as input
windytax = elementary.Label(window)

#Tells our label object to change size based on the size of our window
windytax.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
windytax.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Define what text our window should display
windytax.text = 'Hello Elementary!'

#If we want to see our object we need to tell it to be shown
windytax.show()

#resize_object_add adds our Label object "windytax" to the window
window.resize_object_add(windytax)

#resize takes an ordered pair as input for the size for our window, the dimenions are pixel by pixel
window.resize(300,300)

#Finally lets tell our window object to show up just like we did with our label
window.show()

#Runs when our script is run
if __name__ == "__main__":
#Runs our function which creates our window
hello_elementary()

#Starts an elementary event loop which displays all elementary objects we've created. Our code stays at this point until elementary.exit() is called
elementary.run()

#Once elementary is done running lets shut everything off to finish the application
elementary.shutdown()

In this example we create two elementary objects: A StandardWindow and a Label. The StandardWindow as you can guess is the window we are creating, while the Label is a child object that we add to our window to display.

Example 2:
We want our application to do much more than just display text (most of the time). So let's go ahead and add a couple more objects to our Hello Elementary application. Let's add a button that closes our application:


The full code for this application can be found here. I will now highlight what is different from our previous example.

def hello_elementary():
...

#Create an elementary button object
button = elementary.Button(window)

#Set some text for our button
button.text = "Goodbye Elementary"

#callback_pressed_add tells our button a callback to run when our button is pressed, the first argument is the function run and the following arguments are things to pass to the callback
button.callback_pressed_add(button_pressed, "argument1", "argument2")

#Show our button
button.show()

#Since we now have multiple objects we want to display on our window, we can position these objects using an elementary box which is a container object that you can "pack" items into.

#Create a box
box = elementary.Box(window)

#Tell our box to fill all open space in our window
box.size_hint_weight_set(evas.EVAS_HINT_EXPAND, evas.EVAS_HINT_EXPAND)
box.size_hint_align_set(evas.EVAS_HINT_FILL, evas.EVAS_HINT_FILL)

#Show our box
box.show()

#Lets pack our label and then button into our box!
box.pack_end(windytax)
box.pack_end(button)

#This time lets use our box instead of just our label
window.resize_object_add(box)

#Our callback when the button is pressed. The first argument for this function will be the elementary button object. The rest of the arguments are the custom things we passed above
def button_pressed(button, arg1, arg2):
#Show the content of our arguments in terminal
print arg1, arg2

#Lets have our button close the application, so run:
elementary.exit()

Example 2 adds two more elementary objects to our application - a Box and a Button. A Box is an elementary object that we use to hold other elementary objects to they are positioned how we want them inside our application window. You "pack" items into a box that is either vertical (default) or horizontal. A Button is an object that can have text and/or images displayed on it that can fire a callback when pressed.

Resources for this Lesson:
  • Hello Elementary
  • StandardWindow
  • Label
  • Box
  • Button
~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

  • Ten Linux Distros that use Enlightenment
    In case you haven't noticed I've had a bit of an obsession with the Enlightenment desktop of the late. Even though this desktop is ...
  • Playing Magic the Gathering on Linux with Cockatrice
    If you ask almost anyone that knows me in person they will attest to the fact that I am a huge nerd. Today I would like to give a short tuto...
  • New E17 Stable Snapshot and the First of E18
    You read that title right folks. The first showing of Enlightenment DR18 (or E18 for short) has become a reality. Sure, it is nowhere near w...
  • Twenty Sleek GTK Themes for your Linux Desktop
    Last month I posted about twenty two different icon sets you could use to class up your Linux desktop. Today I would like to share with you...
  • HOWTO: Enable Compiz under Bodhi (Enlightenment)
    One of the reasons I gave for giving the Enlightenment desktop a try was its elegance. While it is true that Enlightenment has a good numbe...
  • 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...
  • Bodhi Linux ARMHF RootFS
    If you've been following my blog (or my updates on Google+ ) then odds are you know I currently have my hands on two ARM devices ( plus ...
  • Linux Out Performs Windows in OpenGL
    Late last year I did a posting detailing how Windows 7 crushed Ubuntu 9.10 in the area of 3D performance. Nine months later I am happy to s...
  • 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...
  • OpenGL vs DirectX - Benchmark Comparison
    I've done a few benchmarks to date and today I am going to add another interesting set of numbers to the list. Unigine is a cross-pla...

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

  • ►  2014 (1)
    • ►  January (1)
  • ▼  2013 (22)
    • ►  December (3)
    • ►  November (1)
    • ►  September (1)
    • ►  June (1)
    • ►  May (2)
    • ►  March (2)
    • ►  February (3)
    • ▼  January (9)
      • New E17 Stable Snapshot and the First of E18
      • Tutorial 1: Hello Elementary
      • Bodhi on MK802 and other ARM Updates
      • Some Yummy Elementary Applications
      • Nexus 7 jams with Bodhi Linux
      • Bodhi Linux 2.2.0 Released
      • Bodhi is ARMing up for a new Year
      • Introducing eAndora - Pandora Client
      • Bodhi Linux gets E17 Stable Packages
  • ►  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