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:
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…
blog comments powered by Disqus |
|
|
|
|
|
|
|