Home arrow Python arrow Page 4 - Mobile Programming in Python using PyS60: UI Controls

PyS60 in Real World - Python

In this discussion I will focus on the basics of using a UI library provided by PyS60. The first section will focus on the types of controls. The second and third sections will be about two basic controls – query and note. In the fourth section, a simple application will be developed that uses the controls discussed in the second and third sections. That’s the agenda for this discussion.

TABLE OF CONTENTS:
  1. Mobile Programming in Python using PyS60: UI Controls
  2. UI Controls: Query and Notes
  3. The Note Control
  4. PyS60 in Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 2
February 27, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Let's see how these controls can be used within an application. The application that's going to be developed is a simple guessing game. It will perform the following tasks:

  1. Choose a random number. 
  2. Ask the user to enter his or her guess.
  3. Compare and show the result.
  4. Ask if he or she will like to continue.

So let's start. First the imports:

from appuifw import *

As you already know, the above statement imports all the classes within the appuifw module. Next, the application has to choose a random number. However, since this will be a repeating process until the user chooses to stop, choosing the random number will be within a loop. The loop will terminate only when the user says he or she would like to stop. The code is as follows:

from appuifw import *


continue_guess=true


while continue_guess:

guess_no=random()

The loop will continue until continue_guess becomes false. Next, the application needs to accept the user’s guess. To accept the value, the query function will be used. The message will be “Enter your guess,” the type will be number (since decimals will not be allowed), and no initial value will be displayed. The value will be saved in user_guess variable. Here is the code:

from appuifw import *


continue_guess=true


while continue_guess:

guess_no=random()

user_guess=query(u”Enter your guess”, ‘number’)

Next comes the comparison and displaying the result functionality. First, the user_guess will be checked for validity (i.e. if the user clicked cancel, it would be null). If it is not null, then it will be compared with the guess_no, and the appropriate number will be displayed. If the user pressed cancel, a “You opted out” will be shown and the continue_guess will be set to false. Here is the code:


from appuifw import *


continue_guess=true


while continue_guess:

guess_no=random()

user_guess=query(u”Enter your guess”, ‘number’)

if user_guess is Null:

note(u”You have opted out”)

continue_guess=false

elif user_guess<guess_no:

note(u”Your guess is lesser than the goal”)

elif user_guess>guess_no:

note(u”Your guess is higher than the goal”)

else:

note(u”Congrats for excellent guess”)


Next comes the code that asks the user whether or not to continue. The query box is shown to the user with “Enter Y to continue and N quit.” If Y is entered, the loop is continued, otherwise the application is exited.


from appuifw import *


continue_guess=true


while continue_guess:

guess_no=random()

user_guess=query(u”Enter your guess”, ‘number’)

if user_guess is Null:

note(u”You have opted out”)

continue_guess=false

elif user_guess<guess_no:

note(u”Your guess is lesser than the goal”)

elif user_guess>guess_no:

note(u”Your guess is higher than the goal”)

else:

note(u”Congrats for excellent guess”)


user_choice=query(u”Enter Y to continue or N to quit”)

 

if user_choice is Null or user_choice==’N’:

continue_guess=false

else:

continue_guess=true


That completes the application. In this discussion, the focus was on simple UI controls. The next part will discuss complex UI controls, such as forms. Till then…



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: