User input/Graphical: Difference between revisions

From Rosetta Code
Content added Content deleted
(C)
Line 119: Line 119:
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
{{works with|Python|2.5}}

{{libheader|Tkinter}}
{{libheader|Tkinter}}
<lang python>import tkSimpleDialog
<lang python>import tkSimpleDialog

Revision as of 11:57, 8 July 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.

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[3];
 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);
 widgs[2] = GTK_WIDGET(win);
 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>

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>

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>

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)

VBScript

<lang vb>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.)

Dialog_Input_1(1, "`User Input example`,
                   `??Enter a string `,
                   `??Enter a number `") 
#2 = Num_Eval_Reg(2)