![]() |
Build a simple Fart App in 30 Mins! N900, PyQt
1 Attachment(s)
The finished app, a list of fart sounds to choose from on the left and a big dangerous red push button on the right..
http://i90.photobucket.com/albums/k2...528-230425.png This is a beginner tutorial in association with the N900 coding competition , running til July 21st. Please get involved :) This tutorial covers: • GUI creation with Qt Designer • Sound playback with phonon • Getting around the lack of a switch/case statement in python • The mystical art of naming fart sounds Pre-requisites: If you are a beginner please go through the starter tutorials from MikeC and FatalSaint first, which are both many times more informative and better thought out than this one and will also help get your environments set up. • MikeC - http://talk.maemo.org/showthread.php?t=43663 • FatalSaint - http://talk.maemo.org/showpost.php?p...8&postcount=59 • Python and PyQt should be installed correctly on your PC and n900 • You should have WinSCP on your PC and OpenSSH client-server on your n900 (or equivalents) for copying the files over. Caveats I am also a beginner, this tutorial is put out there in the hope it helps but should not be seen as well written or particularly ‘good’. Step 1: Go and grab yourself 5 or 6 fart sounds from the internet. A simple google search should find plenty of .wav farting audible goodness, e.g. http://www.fartgreetings.com/fartwavs.asp might be a start. Or you can use the ones I have attached in the zip file. Step 2: Fire up your copy of Qt Designer, from the first dialog choose the Main Window template and set the canvas size to 800*400. We only need two widgets - a List Widget, which is used to select the fart sound, and a Push Button widget to trigger the sound to be played. Drag both widgets onto the canvas, with the list to the left and the button to the right. Space them out and resize them until you’re happy with how they look. For more info on using a layout and use of spacings see FatalSaint’s tutorial linked at the beginning. http://i90.photobucket.com/albums/k2...ithwidgets.png Step 3: Left click on the List Widget, and in the Property Editor change it’s name to ‘soundList’. Then right click on the List Widget and choose “Edit Items” from the mouse’s popup menu. A small window called the “Edit List Widget” will appear, and this is where you enter the fart names that the user will see and click on. Click on the green + symbol on the bottom left corner of the widget and add a name for each of the fart sounds you have gathered from step 1. You can also set the properties for each list item, I have chosen text size 14 and centered. Click OK when finished. http://i90.photobucket.com/albums/k2...ist_widget.png Step 4: Left click on the Push Button widget, and change it’s name to ‘pushbutton’ in the Property Editor. I have also changed the font size to 33 and the text to read “Fart!”. In addition, by clicking on the styleSheet property you can bring up the Style Sheet Editor widget window to assign a colour to the button, in this case I have chosen a dangerous hue of red. http://i90.photobucket.com/albums/k2...tyle_sheet.png Step 5: That is the GUI designed, save (File->Save As) the project as maeFart.ui and close down Qt Designer out of your way. Next we use pyuic4 to generate our python code. Open up a command line prompt, and CD into the folder you have saved the maeFart.ui file. From there, type: Code:
pyuic4 maeFart.ui -o maeFart.py Step 6: We now have our python GUI, we need a main to call it. Create a new python file called main.py and copy in the following code: Code:
#!/usr/bin/python2.5 Step7: Qt uses the Phonon multimedia framework and can playback most of the common multimedia formats. The architecture of Phonon is that, when playing back audio, you create a media object and connect it via a path to an audio output node, which ouputs audio to the sound card or device. http://www.riverbankcomputing.co.uk/...nceptaudio.png First step to using phonon is to import it into our project, so at the top add: Code:
from PyQt4.phonon import Phonon Code:
self.m_media = Phonon.MediaObject(self) Connecting the button. We will write a method called playFart(), triggered by the pushButton, that will play the fart sound. First we need to hook up the “clicked()” signal of the button to call the method as so: Code:
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.playFart) Implementing the playFart method by adding the following code to the main.py file Code:
def playFart(self): Code:
self.farts={ In this case the dictionary only contains keys and the associated filenames as the sound files will be placed in the same folder as the main.py file, otherwise the path would also need to be added to the filenames. Step 10: The completed files maeFart.py and main.py should look as follows, test them out locally and make sure everything is ok. main.py Code:
#!/usr/bin/python2.5 Code:
# -*- coding: utf-8 -*- Use WinSCP and OpenSSH to transfer the files into your N900’s Opt directory. To run, open Xterm and cd into the opt directory and run the main.py file. Code:
cd /opt http://i90.photobucket.com/albums/k2...528-230425.png That's it for now.. happy farting! |
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Nice. :) I'd like to learn how to do that kind of stuff.
|
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Great app. Even better than you last ones for n9 ;)
Great start if anyone trying to start a PyQt career. Do you want me to port it n9? |
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Quote:
Might give this a go, might put some wind into my sails to start developing on a small scale. :rolleyes: |
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Quote:
(*) : no vkb, small fonts, etc. |
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Quote:
And yes, you should use QtQuick if possible, due to the issues QtWidgets have as mentioned by rcolistete. |
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Wow with the help of kojackers nobel ideas and his innovative coding skills my n900 now can fart :D wish it could produce pleasent aromatic feeling also:D just a feature request : using the front camera or back camera to fart. So that i can keep the phone on sofa and when somebody sits beside it would fart:D
|
Re: Build a simple Fart App in 30 Mins! N900, PyQt
Quote:
Using a camera would need a bunch of image processing, I guess, to detect the person moving past. Even easier might be to leave the phone on the sofa and use the accelerometer code from maeHammer (part 2 of the tutorial here - 'rt2 - adding sound and accelerometer with maeHammer') to play back the noise when the phone is moved/jolted when the victim sits down. Just make sure you don't hide the phone too well or the person may sit down on top of it ;) |
All times are GMT. The time now is 21:43. |
vBulletin® Version 3.8.8