User input/Graphical: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Oz example.)
Line 306: Line 306:
value = parseInt( prompt("Enter the number 75000") );
value = parseInt( prompt("Enter the number 75000") );
}</lang>
}</lang>

=={{header|Oz}}==
Shows a dialog that asks for both a string and a number.
Does not allow to close the dialog until 75000 was entered.
Note: "td" is short for "topdown", "lr" for "leftright".
<lang oz>functor
import
Application
QTk at 'x-oz://system/wp/QTk.ozf'
System
define
Number NumberWidget
Text
StatusLabel
WindowClosed
GUI = td(action:OnClose
return:WindowClosed
lr(label(text:"Enter some text:" width:20)
entry(return:Text glue:ew)
glue:ew)
lr(label(text:"Enter a number:" width:20)
numberentry(max:100000 return:Number handle:NumberWidget)
label(handle:StatusLabel width:20)
glue:ew
)
button(text:"Ok" glue:ew
action:OnClose
)
)
proc {OnClose}
if {NumberWidget get($)} \= 75000 then
{StatusLabel set(text:"Invalid value")}
else
{Window close}
end
end
Window = {QTk.build GUI}
{Window show}
{Wait WindowClosed}
{System.showInfo "You entered; "#Text#", "#Number}
{Application.exit 0}
end</lang>


=={{header|Perl}}==
=={{header|Perl}}==

Revision as of 13:58, 19 December 2009

Task
User input/Graphical
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to input a string and the integer 75000, from graphical user interface.

See also: User Input - text

Ada

Library: GTK version GtkAda
Library: GtkAda

<lang ada>with Gtk.Button; use Gtk.Button; with Gtk.GEntry; use Gtk.GEntry; with Gtk.Label; use Gtk.Label; with Gtk.Window; use Gtk.Window; with Gtk.Widget; use Gtk.Widget; with Gtk.Table; use Gtk.Table;

with Gtk.Handlers; with Gtk.Main;

procedure Graphic_Input is

  Window  : Gtk_Window;
  Grid    : Gtk_Table;
  Label   : Gtk_Label;
  Message : Gtk_Label;
  Edit    : Gtk_GEntry;
  Button  : Gtk_Button;

  package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
  package Return_Handlers is
     new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);

  function Delete_Event (Widget : access Gtk_Widget_Record'Class)
     return Boolean is
  begin
     return False;
  end Delete_Event;

  procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
  begin
     Gtk.Main.Main_Quit;
  end Destroy;

  procedure Clicked (Widget : access Gtk_Widget_Record'Class) is
  begin
     if Get_Text (Label) = "Enter integer:" then
        Set_Text (Message, "Entered:" & Integer'Image (Integer'Value (Get_Text (Edit))));
        Set_Sensitive (Button, False);
     else
        Set_Text (Message, "Entered:" & Get_Text (Edit));
        Set_Text (Label, "Enter integer:");
     end if;
  exception
     when Constraint_Error =>
        Set_Text (Message, "Error integer input");
  end Clicked;

begin

  Gtk.Main.Init;
  Gtk.Window.Gtk_New (Window);
  Gtk_New (Grid, 2, 3, False);
  Add (Window, Grid);
  Gtk_New (Label, "Enter string:");
  Attach (Grid, Label, 0, 1, 0, 1);
  Gtk_New (Edit);   
  Attach (Grid, Edit, 1, 2, 0, 1);
  Gtk_New (Button, "OK");   
  Attach (Grid, Button, 2, 3, 0, 1);
  Gtk_New (Message);
  Attach (Grid, Message, 0, 3, 1, 2);
  Return_Handlers.Connect
  (  Window,
     "delete_event",
     Return_Handlers.To_Marshaller (Delete_Event'Access)
  );
  Handlers.Connect
  (  Window,
     "destroy",
     Handlers.To_Marshaller (Destroy'Access)
  );
  Handlers.Connect
  (  Button,
     "clicked",
     Handlers.To_Marshaller (Clicked'Access)
  );
  Show_All (Grid);
  Show (Window);

  Gtk.Main.Main;

end Graphic_Input;</lang>

AppleScript

<lang applescript>set input to text returned of

   (display dialog "Enter text:" default answer "")</lang>

<lang applescript>set input to text returned of

   (display dialog "Enter a number:" default answer "")
 as integer</lang>

AutoHotkey

InputBox

<lang AutoHotkey>InputBox, String, Input, Enter a string: InputBox, Int, Input, Enter an int: Msgbox, You entered "%String%" and "%Int%"</lang>

Gui Edit

<lang AutoHotkey>Gui, Add, Text,, String: Gui, Add, Text,, Int: Gui, Add, Button, gGo, Go! Gui, Add, Edit, vString ym Gui, Add, Edit, VInt Gui, Show, Center, Input Return

Go: Gui, Submit, NoHide Msgbox, You entered "%String%" and "%Int%" ExitApp Return</lang>

C

Library: GTK

<lang c>#include <gtk/gtk.h>

void ok_hit(GtkButton *o, GtkWidget **w) {

 GtkMessageDialog *msg;
 gdouble v = gtk_spin_button_get_value((GtkSpinButton *)w[1]);
 const gchar *c = gtk_entry_get_text((GtkEntry *)w[0]);
 msg = (GtkMessageDialog *)
   gtk_message_dialog_new(NULL,

GTK_DIALOG_MODAL, (v==75000) ? GTK_MESSAGE_INFO : GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "You wrote '%s' and selected the number %d%s", c, (gint)v, (v==75000) ? "" : " which is wrong (75000 expected)!");

 gtk_widget_show_all(GTK_WIDGET(msg));
 (void)gtk_dialog_run(GTK_DIALOG(msg));
 gtk_widget_destroy(GTK_WIDGET(msg));
 if ( v==75000 ) gtk_main_quit();

}

int main(int argc, char **argv) {

 GtkWindow *win;
 GtkEntry *entry;
 GtkSpinButton *spin;
 GtkButton *okbutton;
 GtkLabel *entry_l, *spin_l;
 GtkHBox *hbox[2];
 GtkVBox *vbox;
 GtkWidget *widgs[2];
 gtk_init(&argc, &argv);
 
 win = (GtkWindow *)gtk_window_new(GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title(win, "Insert values");
 
 entry_l = (GtkLabel *)gtk_label_new("Insert a string");
 spin_l =  (GtkLabel *)gtk_label_new("Insert 75000");
 entry = (GtkEntry *)gtk_entry_new();
 spin = (GtkSpinButton *)gtk_spin_button_new_with_range(0, 80000, 1);
 widgs[0] = GTK_WIDGET(entry);
 widgs[1] = GTK_WIDGET(spin);
 okbutton = (GtkButton *)gtk_button_new_with_label("Ok");
 
 hbox[0] = (GtkHBox *)gtk_hbox_new(FALSE, 1);
 hbox[1] = (GtkHBox *)gtk_hbox_new(FALSE, 1);
 vbox = (GtkVBox *)gtk_vbox_new(TRUE, 1);
 gtk_container_add(GTK_CONTAINER(hbox[0]), GTK_WIDGET(entry_l));
 gtk_container_add(GTK_CONTAINER(hbox[0]), GTK_WIDGET(entry));
 gtk_container_add(GTK_CONTAINER(hbox[1]), GTK_WIDGET(spin_l));
 gtk_container_add(GTK_CONTAINER(hbox[1]), GTK_WIDGET(spin));
 gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(hbox[0]));
 gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(hbox[1]));
 gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(okbutton));
 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(vbox));
 g_signal_connect(G_OBJECT(win), "delete-event", (GCallback)gtk_main_quit, NULL);
 g_signal_connect(G_OBJECT(okbutton), "clicked", (GCallback)ok_hit, widgs);
 gtk_widget_show_all(GTK_WIDGET(win));
 gtk_main();
 return 0;

}</lang>

Common Lisp

Library: CAPI
Works with: LispWorks

Prompt for a string:

<lang lisp>(capi:prompt-for-string "Enter a string:")</lang>

Repeatedly prompt for an integer until either the user presses 'Cancel' (instead of 'OK') or the integer is 75,000.

<lang lisp>(do ((number 0) (okp t))

   ((or (not okp) (= 75000 number)))
 (multiple-value-setq (number okp)
     (capi:prompt-for-integer "Enter an integer:")))</lang>

Alternatively, display a prompt where the 'OK' button will not be enabled until the input is 75,000:

<lang lisp>(capi:prompt-for-integer "Enter an integer:"

                        :ok-check #'(lambda (n) (= n 75000)))</lang>

And a version which displays one prompt with an area for a string and an area for an integer, and only enables the 'OK' button when the integer is 75,000.

First, define an interface with the text-areas: <lang lisp>(capi:define-interface string/integer-prompt () ()

 (:panes
  (string-pane
   capi:text-input-pane
   :title "Enter a string:")
  (integer-pane
   capi:text-input-pane
   :title "Enter an integer:"
   :change-callback :redisplay-interface))
 (:layouts
  (main
   capi:column-layout
   '(string-pane integer-pane))))</lang>

Then a function to extract the string and integer: <lang lisp>(defun string/integer-prompt-value (pane)

 (with-slots (string-pane integer-pane) pane
   (let* ((string (capi:text-input-pane-text string-pane))
          (integer-string (capi:text-input-pane-text integer-pane))
          (integer (when (every 'digit-char-p integer-string)
                     (parse-integer integer-string :junk-allowed t))))
     (values (cons string integer)))))</lang>

Finally, display a prompt using the defined function to extract a value, and an 'ok-check' to ensure that the integer value is 75,000. <lang lisp>(defun do-prompting ()

 (capi:popup-confirmer
  (make-instance 'string/integer-prompt)
  "Enter some values:"
  :value-function 'string/integer-prompt-value
  :ok-check #'(lambda (result) (eql (cdr result) 75000))))</lang>

J

A revision of the script posted at the Simple_Windowed_Application

<lang j>SIMPLEGUI=: 0 : 0 pc simpleGui; xywh 136 39 44 12;cc close button;cn "Close"; xywh 0 14 60 11;cc IntegerLabel static ss_right;cn "Enter an integer"; xywh 65 13 60 12;cc integer edit; xywh 0 39 60 11;cc TextLabel static ss_right;cn "Enter text"; xywh 64 38 60 12;cc text edit; pas 6 6;pcenter; rem form end; )

simpleGui_run=: 3 : 0 wd SIMPLEGUI simpleGui_integer=: '75000' NB. initialize integer wd 'set integer *', ": simpleGui_integer wd 'pshow;' )

simpleGui_close=: 3 : 0 simpleGui_text =: text try.

 simpleGui_integer =: ". integer

catch.

 wdinfo 'simpleGui_integer is Not a number'

end. wd'pclose' )

simpleGui_close_button=: 3 : 0 simpleGui_close )

simpleGui_cancel =: simpleGui_close

simpleGui_run</lang>

After you enter values into the graphical display and click "Close", then you can bring focus to the .ijx window to see results. Then you can type "simpleGui_integer 'return'" and "simpleGui_text 'return'" into the .ijx window to verify their values.

Java

Library: Swing

<lang java>import javax.swing.*;

public class GetInputSwing {

   public static void main(String[] args) throws Exception {
       int number = Integer.parseInt(
               JOptionPane.showInputDialog ("Enter an Integer"));
       String string = JOptionPane.showInputDialog ("Enter a String");
   }

}</lang>

JavaScript

Works with: FireFox

or any JavaScript-enabled browser

<lang javascript>var str = prompt("Enter a string"); var value = 0; while (value != 75000) {

   value = parseInt( prompt("Enter the number 75000") );

}</lang>

Oz

Shows a dialog that asks for both a string and a number. Does not allow to close the dialog until 75000 was entered. Note: "td" is short for "topdown", "lr" for "leftright". <lang oz>functor import

  Application
  QTk at 'x-oz://system/wp/QTk.ozf'
  System

define

  Number NumberWidget
  Text
  StatusLabel
  WindowClosed
  GUI = td(action:OnClose

return:WindowClosed lr(label(text:"Enter some text:" width:20) entry(return:Text glue:ew) glue:ew) lr(label(text:"Enter a number:" width:20) numberentry(max:100000 return:Number handle:NumberWidget) label(handle:StatusLabel width:20) glue:ew ) button(text:"Ok" glue:ew action:OnClose ) )

  proc {OnClose}
     if {NumberWidget get($)} \= 75000 then

{StatusLabel set(text:"Invalid value")}

     else

{Window close}

     end
  end
  Window = {QTk.build GUI}
  {Window show}
  {Wait WindowClosed}
  {System.showInfo "You entered; "#Text#", "#Number}
  {Application.exit 0}

end</lang>

Perl

Library: wxWidgets

<lang perl>use Wx;

  1. ---------------------------------------------------------------

package MyApp; use base 'Wx::App'; use Wx qw(wxHORIZONTAL wxVERTICAL wxALL wxALIGN_CENTER); use Wx::Event 'EVT_BUTTON';

our ($frame, $text_input, $integer_input);

sub OnInit

  {$frame = new Wx::Frame
      (undef, -1, 'Input window', [-1, -1], [250, 150]);
   my $panel = new Wx::Panel($frame, -1);
   $text_input = new Wx::TextCtrl($panel, -1, );
   $integer_input = new Wx::SpinCtrl
      ($panel, -1, , [-1, -1], [-1, -1],
       0, 0, 100_000);
   my $okay_button = new Wx::Button($panel, -1, 'OK');
   EVT_BUTTON($frame, $okay_button, \&OnQuit);
   my $sizer = new Wx::BoxSizer(wxVERTICAL);
   $sizer->Add($_, 0, wxALL | wxALIGN_CENTER, 5)
       foreach $text_input, $integer_input, $okay_button;
   $panel->SetSizer($sizer);
   $frame->Show(1);}

sub OnQuit

  {print 'String: ', $text_input->GetValue, "\n";
   print 'Integer: ', $integer_input->GetValue, "\n";
   $frame->Close;}
  1. ---------------------------------------------------------------

package main;

MyApp->new->MainLoop;</lang>

Python

Works with: Python version 2.5
Library: Tkinter

<lang python>import tkSimpleDialog

number = tkSimpleDialog.askinteger("Integer", "Enter a Number") string = tkSimpleDialog.askstring("String", "Enter a String")</lang>

REBOL

<lang REBOL>REBOL [ Title: "Graphical User Input" Author: oofoe Date: 2009-12-07 URL: http://rosettacode.org/wiki/User_Input_-_graphical ]

Simple GUI's can be defined with 'layout', a special purpose dialect
for specifying interfaces. In this case, I describe a gradient
background with an instruction label, followed by the input fields
and a validation button. It's possible to check dynamically as the
user types but I wanted to keep this example as clear as possible.

view layout [

You can define new widget styles. Here I create a padded label style
(so that everything will line up) and a simple indicator light to
show if there's a problem with an input field.

style label vtext 60 "unlabeled" style indicator box maroon 24x24

backdrop effect [gradient 0x1 black coal]

vtext "Please enter a string, and the number 75000:"

By default, GUI widgets are arranged top down. The 'across' word
starts stacking widgets from left to right. 'return' starts a new
line -- just like on a typewriter!

across

Notice I'm using my new label and indicator styles here. Widgets
that I need to access later (the input field and the indicator) are
assigned to variables.

label "string:" s: field 240 si: indicator return

label "number:" n: field 50 ni: indicator return

pad 66 button "validate" [

The user may have entered bogus values, so I reset the indicators

si/color: ni/color: maroon

Now I check to see if the values are correct. For the string, I just
care that there is one. For the integer, I make sure that it
evaluates to an integer and that it's value is 75000. Because I've
already set the indicator colour, I don't care the integer
conversion raises an error or not, so I ignore it if anything goes
wrong.

if 0 < length? get-face s [si/color: green] error? try [if 75000 = to-integer get-face n [ni/color: green]]

show [si ni] ; Repainting multiple objects at once. ] ]</lang>

Ruby

Unlike most other solutions, this validates the input number to be 75,000.

Library: Ruby/Tk

<lang ruby>require 'tk'

def main

 root = TkRoot.new
 l1 = TkLabel.new(root, "text" => "input a string")
 e1 = TkEntry.new(root)
 l2 = TkLabel.new(root, "text" => "input the number 75000")
 e2 = TkEntry.new(root) do
   validate "focusout"
   validatecommand lambda {e2.value.to_i == 75_000}
   invalidcommand  lambda {focus_number_entry(e2)}
 end
 ok = TkButton.new(root) do
   text "OK"
   command lambda {validate_input(e1, e2)}
 end
 Tk.grid(l1, e1)
 Tk.grid(l2, e2)
 Tk.grid("x",ok, "sticky" => "w")  
 Tk.mainloop

end

def validate_input(text_entry, number_entry)

 if number_entry.value.to_i != 75_000
   focus_number_entry(number_entry)
 else
   puts %Q{You entered: "#{text_entry.value}" and "#{number_entry.value}"}
   root.destroy
 end

end

def focus_number_entry(widget)

 widget \
   .configure("background" => "red", "foreground" => "white") \
   .selection_range(0, "end") \
   .focus

end

main</lang>

Tcl

Library: Tk

<lang tcl># create entry widget: pack [entry .e1]

  1. read its content:

set input [.e get]</lang>

Alternatively, the content of the widget can be tied to a variable: <lang tcl>pack [entry .e1 -textvar input]

  1. show the content at any time by

puts $input</lang> The -validate option can be used to test the contents/edits of the widget at any time against any parameters (including testing string is integer when the user hits <Return> or such)

TI-89 BASIC

This program leaves the requested values in the global variables s and n.

<lang ti89b>Prgm

 Dialog
   Title "Rosetta Code"
   Request "A string", s
   DropDown "An integer", {"75000"}, n
 EndDlog
 74999 + n → n

EndPrgm</lang>

VBScript

<lang vbscript>strUserIn = InputBox("Enter Data") Wscript.Echo strUserIn</lang>

Vedit macro language

Displays a dialog box with two input fields and default OK button. The values entered are stored in text registers 1 and 2. The value from 2nd field is then converted into numeric value. (Accepts integers or integer expressions.)

<lang vedit>Dialog_Input_1(1, "`User Input example`,

                  `??Enter a string `,
                  `??Enter a number `")</lang>
#2 = Num_Eval_Reg(2)