Hello world/Graphical

From Rosetta Code

Jump to: navigation, search
Task
Hello world/Graphical
You are encouraged to solve this task according to the task description, using any language you may know.

In this User Output task, the goal is to display the string "Goodbye, World!" on a GUI object (alert box, plain window, text area, etc.).

See also: User Output - text

Contents

[edit] ActionScript

 
var textField:TextField = new TextField();
stage.addChild(textField);
textField.text = "Goodbye, World!"
 

[edit] Ada

Library: GTK Library: GtkAda

with Gdk.Event;   use Gdk.Event;
with Gtk.Label; use Gtk.Label;
with Gtk.Window; use Gtk.Window;
with Gtk.Widget; use Gtk.Widget;
 
with Gtk.Handlers;
with Gtk.Main;
 
procedure Windowed_Goodbye_World is
Window : Gtk_Window;
Label  : Gtk_Label;
 
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;
 
begin
Gtk.Main.Init;
Gtk.Window.Gtk_New (Window);
Gtk_New (Label, "Goodbye, World!");
Add (Window, Label);
Return_Handlers.Connect
( Window,
"delete_event",
Return_Handlers.To_Marshaller (Delete_Event'Access)
);
Handlers.Connect
( Window,
"destroy",
Handlers.To_Marshaller (Destroy'Access)
);
Show_All (Label);
Show (Window);
 
Gtk.Main.Main;
end Windowed_Goodbye_World;

[edit] AppleScript

display dialog "Goodbye, World!" buttons {"Bye"}

[edit] AutoHotkey

MsgBox, Goodbye`, World!
ToolTip, Goodbye`, World!
Gui, Add, Text,   x4    y4,   To be announced:
Gui, Add, Edit, xp+90 yp-3, Goodbye, World!
Gui, Add, Button, xp+98 yp-1, OK
Gui, Show, w226 h22 , Rosetta Code
Return
SplashTextOn, 100, 100, Rosetta Code, Goodbye, World!

[edit] AutoIt

; display a window
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
 
$hMain = GUICreate("", 178, 125, -1, -1)
GUICtrlCreateLabel("Goodbye, world", 48, 48, 78, 17)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($hMain)
ExitLoop
EndSwitch
WEnd
 
;display a message box
MsgBox(0, "", "Goodbye, world")
 

[edit] BASIC

Works with: FreeBASIC

' Demonstrate a simple Windows application using FreeBasic
 
#include once "windows.bi"
 
Declare Function WinMain(ByVal hInst As HINSTANCE, _
ByVal hPrev As HINSTANCE, _
ByVal szCmdLine as String, _
ByVal iCmdShow As Integer) As Integer
End WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )
 
Function WinMain (ByVal hInst As HINSTANCE, _
ByVal hPrev As HINSTANCE, _
ByVal szCmdLine As String, _
ByVal iCmdShow As Integer) As Integer
MessageBox(NULL, "Goodbye World", "Goodbye World", MB_ICONINFORMATION)
function = 0
End Function

[edit] C

Library: GTK

#include <gtk/gtk.h>
 
int main (int argc, char **argv) {
GtkWidget *window;
gtk_init(&argc, &argv);
 
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Goodbye, World");
g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
gtk_widget_show_all (window);
 
gtk_main();
return 0;
}

Library: Win32 Where hWnd is a valid window handle corresponding to a control in the application

#include "windows.h"
void SayGoodbyeWorld(HWND hWnd)
{
SetWindowText(hWnd, _T("Goodbye, World!"));
}

[edit] C#

Winforms

using System;
using System.Windows.Forms;
 
class Program {
static void Main(string[] args) {
Application.EnableVisualStyles(); //Optional.
MessageBox.Show("Hello World!");
}
}

Gtk

using Gtk;
using GtkSharp;
 
public class GoodbyeWorld {
public static void Main(string[] args) {
Gtk.Window window = new Gtk.Window();
window.Title = "Goodbye, World";
window.DeleteEvent += delegate { Application.Quit(); };
window.ShowAll();
Application.Run();
}
}

[edit] C++

Works with: GCC version 3.3.5

Library: GTK

#include <gtkmm.h>
int main(int argc, char *argv[])
{
Gtk::Main app(argc, argv);
Gtk::MessageDialog msg("Goodbye, World!");
msg.run();
}

Library: Win32 All Win32 APIs work in C++ the same way as they do in C. See the C example.

Library: MFC Where pWnd is a pointer to a CWnd object corresponding to a valid window in the application.

#include "afx.h"
void ShowGoodbyeWorld(CWnd* pWnd)
{
pWnd->SetWindowText(_T("Goodbye, World!"));
}

Library: FLTK

 
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
 
int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(300,180);
Fl_Box *box = new Fl_Box(20,40,260,100,"Goodby, World!");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show(argc, argv);
return Fl::run();
}
 

[edit] Clean

Library: Object I/O

import StdEnv, StdIO
 
Start :: *World -> *World
Start world = startIO NDI Void (snd o openDialog undef hello) [] world
where
hello = Dialog "" (TextControl "Goodbye, World!" [])
[WindowClose (noLS closeProcess)]

[edit] Clojure

(ns experimentation.core
(:import (javax.swing JOptionPane JFrame JTextArea JButton)
(java.awt FlowLayout)))
 
(JOptionPane/showMessageDialog nil "Goodbye, World!")
(let [button (JButton. "Goodbye, World!")
window (JFrame. "Goodbye, World!")
text (JTextArea. "Goodbye, World!")]
(doto window
(.setLayout (FlowLayout.))
(.add button)
(.add text)
(.pack)
(.setDefaultCloseOperation (JFrame/EXIT_ON_CLOSE))
(.setVisible true)))

[edit] COBOL

This is a TUI, not a GUI version, because it Works with: OpenCOBOL 1.1

The program gets the lines and columns of the screen and positions the text in the middle. Program waits for a return key.

	program-id. ghello.
data division.
working-storage section.
01 var pic x(1).
01 lynz pic 9(3).
01 colz pic 9(3).
01 msg pic x(15) value "Goodbye, world!".
procedure division.
accept lynz from lines end-accept
divide lynz by 2 giving lynz.
accept colz from columns end-accept
divide colz by 2 giving colz.
subtract 7 from colz giving colz.
display msg
at line number lynz
column number colz
end-display
accept var end-accept
stop run.

[edit] Common Lisp

This can be done using the extension package ltk that provides an interface to the Tk library. Library: Tk

(use-package :ltk)
 
(defun show-message (text)
"Show message in a label on a Tk window"
(with-ltk ()
(let* ((label (make-instance 'label :text text))
(button (make-instance 'button :text "Done"
:command (lambda ()
(ltk::break-mainloop)
(ltk::update)))))
(pack label :side :top :expand t :fill :both)
(pack button :side :right)
(mainloop))))
 
(show-message "Goodbye World")

[edit] D

Library: gtkD

import gtk.MainWindow;
import gtk.Label;
import gtk.Main;
 
class GoodbyeWorld : MainWindow
{
this()
{
super("GtkD");
add(new Label("Goodbye World"));
showAll();
}
}
 
void main(string[] args)
{
Main.init(args);
new GoodbyeWorld();
Main.run();
}

[edit] Dylan

(This works entered into the interactive shell):

notify-user("hello world!", frame: make(<frame>));

[edit] E

Library: SWT

This is a complete application. If it were part of a larger application, the portions related to interp would be removed.

def <widget> := <swt:widgets.*>
def SWT := <swt:makeSWT>
 
def frame := <widget:makeShell>(currentDisplay)
frame.setText("Rosetta Code")
frame.setBounds(30, 30, 230, 60)
frame.addDisposeListener(def _ { to widgetDisposed(event) {
interp.continueAtTop()
}})
 
def label := <widget:makeLabel>(frame, SWT.getLEFT())
label.setText("Goodbye, World!")
swtGrid`$frame: $label`
 
frame.open()
 
interp.blockAtTop()

[edit] eC

MessageBox:

import "ecere"
MessageBox goodBye { contents = "Goodbye, World!" };

Label:

import "ecere"
Label label { text = "Goodbye, World!", hasClose = true, opacity = 1, size = { 320, 200 } };

Titled Form + Surface Output:

import "ecere"
 
class GoodByeForm : Window
{
text = "Goodbye, World!";
size = { 320, 200 };
hasClose = true;
 
void OnRedraw(Surface surface)
{
surface.WriteTextf(10, 10, "Goodbye, World!");
}
}
 
GoodByeForm form {};

[edit] F#

Just display the text in a message box.

#light
open System
open System.Windows.Forms
[<EntryPoint>]
let main _ =
MessageBox.Show("Hello World!") |> ignore
0

[edit] Factor

To be pasted in the listener :

   USE: ui ui.gadgets.labels
   [ "Goodbye World" <label> "Rosetta Window" open-window ] with-ui

[edit] Forth

Works with: SwiftForth

HWND z" Goodbye, World!" z" (title)" MB_OK MessageBox

[edit] Haskell

Useing Library: gtk from HackageDB

import Graphics.UI.Gtk
import Control.Monad
 
messDialog = do
initGUI
dialog <- messageDialogNew Nothing [] MessageInfo ButtonsOk "Goodbye, World!"
 
rs <- dialogRun dialog
when (rs == ResponseOk || rs == ResponseDeleteEvent) $ widgetDestroy dialog
 
dialog `onDestroy` mainQuit
 
mainGUI

Run in GHCi interpreter:

*Main> messDialog

[edit] HicEst

WRITE(Messagebox='!') 'Goodbye, World!'

[edit] Icon and Unicon

[edit] Icon

link graphics
procedure main()
WOpen("size=100,20") | stop("No window")
WWrites("Goodbye, World!")
WDone()
end

Library: Icon Programming Library graphics is required

[edit] Unicon

This Icon solution works in Unicon.

This example is in need of improvement:
Unicon implemented additional graphical features and a better example may be possible.

[edit] Ioke

Translation of: Java

import(
 :javax:swing, :JOptionPane, :JFrame, :JTextArea, :JButton
)
import java:awt:FlowLayout
 
JOptionPane showMessageDialog(nil, "Goodbye, World!")
button = JButton new("Goodbye, World!")
text = JTextArea new("Goodbye, World!")
window = JFrame new("Goodbye, World!") do(
layout = FlowLayout new
add(button)
add(text)
pack
setDefaultCloseOperation(JFrame field:EXIT_ON_CLOSE)
visible = true
)

[edit] J

wdinfo'Goodbye, World!'

[edit] Java

Library: Swing

import javax.swing.*;
public class OutputSwing {
public static void main(String[] args) throws Exception {
JOptionPane.showMessageDialog (null, "Goodbye, World!");//alert box
JFrame window = new JFrame("Goodbye, World!");//text on title bar
JTextArea text = new JTextArea();
text.setText("Goodbye, World!");//text in editable area
JButton button = new JButton("Goodbye, World!");//text on button
 
//so the button and text area don't overlap
window.setLayout(new FlowLayout());
window.add(button);//put the button on first
window.add(text);//then the text area
 
window.pack();//resize the window so it's as big as it needs to be
 
window.setVisible(true);//show it
//stop the program when the window is closed
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

[edit] JavaScript

Works with: Firefox version 2.0

This pops up a small dialog, so it might be termed GUI display.

 alert("Goodbye, World!");

[edit] Liberty BASIC

NOTICE "Goodbye, world!"

[edit] Logo

Works with: UCB Logo

Among the turtle commands are some commands for drawing text in the graphical area. Details and capabilities differ among Logo implementations.

LABEL [Hello, World!]
SETLABELHEIGHT 2 * last LABELSIZE
LABEL [Goodbye, World!]

[edit] Lua

require "iuplua"
 
dlg = iup.dialog{iup.label{title="Goodbye, World!"}; title="test"}
dlg:show()
 
if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
iup.MainLoop()
end

[edit] MAXScript

messageBox "Goodbye world"

[edit] Modula-3

Library: Trestle

MODULE GUIHello EXPORTS Main;
 
IMPORT TextVBT, Trestle;
 
<*FATAL ANY*>
 
VAR v := TextVBT.New("Goodbye, World!");
 
BEGIN
Trestle.Install(v);
Trestle.AwaitDelete(v);
END GUIHello.

This code requires an m3makefile.

import ("ui")
implementation ("GUIHello")
program ("Hello")

This tells the compiler to link with the UI library, the file name of the implementation code, and to output a program named "Hello".

[edit] Objective-C

To show a modal alert:

NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:@"Goodbye, World!"];
[alert runModal];

[edit] OCaml

Library: GTK

let delete_event evt = false
 
let destroy () = GMain.Main.quit ()
 
let main () =
let window = GWindow.window in
let _ = window#set_title "Goodbye, World" in
let _ = window#event#connect#delete ~callback:delete_event in
let _ = window#connect#destroy ~callback:destroy in
let _ = window#show () in
GMain.Main.main ()
;;
 
let _ = main () ;;

Library: OCaml-Xlib

Library: Tk

ocaml -I +labltk labltk.cma

Just output as a label in a window:

let () =
let main_widget = Tk.openTk () in
let lbl = Label.create ~text:"Goodbye, World" main_widget in
Tk.pack [lbl];
Tk.mainLoop();;

Output as text on a button that exits the current application:

let () =
let action () = exit 0 in
let main_widget = Tk.openTk () in
let bouton_press =
Button.create main_widget ~text:"Goodbye, World" ~command:action in
Tk.pack [bouton_press];
Tk.mainLoop();;

[edit] Oz

declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
Window = {QTk.build td(label(text:"Goodbye, World!"))}
in
{Window show}

[edit] Perl

Works with: Perl version 5.8.8

Library: Tk Just output as a label in a window:

use Tk;
 
$main = MainWindow->new;
$main->Label(-text => 'Goodbye, World')->pack;
MainLoop();

Output as text on a button that exits the current application:

use Tk;
 
$main = MainWindow->new;
$main->Button(
-text => 'Goodbye, World',
-command => \&exit,
)->pack;
MainLoop();

Library: Gtk2

use Gtk2 '-init';
 
$window = Gtk2::Window->new;
$window->set_title('Goodbye world');
$window->signal_connect(
destroy => sub { Gtk2->main_quit; }
);
 
$label = Gtk2::Label->new('Goodbye, world');
$window->add($label);
 
$window->show_all;
Gtk2->main;

Library: XUL::GuiGui

use XUL::Gui;
 
display Label 'Goodbye, World!';
use XUL::Gui;
 
display Button
label => 'Goodbye, World!',
oncommand => sub {quit};

[edit] PHP

Library: PHP-GTK

if (!class_exists('gtk')) 
{
die("Please load the php-gtk2 module in your php.ini\r\n");
}
 
$wnd = new GtkWindow();
$wnd->set_title('Goodbye world');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
 
$lblHello = new GtkLabel("Goodbye, World!");
$wnd->add($lblHello);
 
$wnd->show_all();
Gtk::main();

[edit] PicoLisp

(call 'dialog "--msgbox" "Goodbye, World!" 5 20)

[edit] PostScript

In the geenral Postscript context, the show command will render the string that is topmost on the stack at the currentpoint in the previously setfont. Thus a minimal PostScript file that will print on a PostScript printer or previewer might look like this:

%!PS
% render in Helvetica, 12pt:
/Helvetica findfont 12 scalefont setfont
% somewhere in the lower left-hand corner:
50 dup moveto
% render text
(Goodbye World) show
% wrap up page display:
showpage

[edit] PowerBASIC

Works with: PB/Win

FUNCTION PBMAIN() AS LONG
MSGBOX "Goodbye, World!"
END FUNCTION

[edit] PowerShell

Library: WPK
Works with: PowerShell version 2

New-Label "Goodbye, World!" -FontSize 24 -Show

Library: Windows Forms

$form = New-Object System.Windows.Forms.Form
$label = New-Object System.Windows.Forms.Label
 
$label.Text = "Goodbye, World!"
$form.AutoSize = $true
$form.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
$form.Controls.Add($label)
 
$Form.ShowDialog() | Out-Null

Alternatively, simply as a message box:

Library: Windows Forms

[System.Windows.Forms.MessageBox]::Show("Goodbye, World!")

[edit] PureBasic

MessageRequester("Hello","Goodbye, World!")

Using the Windows API:

MessageBox_(#Null,"Goodbye, World!","Hello")

[edit] Python

Works with: Python version 2.5

Library: Tkinter

import tkMessageBox
 
result = tkMessageBox.showinfo("Some Window Label", "Goodbye, World!")

Note: The result is a string of the button that was pressed.

Library: PyQt

import PyQt4.QtGui
app = PyQt4.QtGui.QApplication([])
pb = PyQt4.QtGui.QPushButton('Hello World')
pb.connect(pb,PyQt4.QtCore.SIGNAL("clicked()"),pb.close)
pb.show()
exit(app.exec_())

Library: PyGTK

import pygtk
pygtk.require('2.0')
import gtk
 
window = gtk.Window()
window.set_title('Goodbye, World')
window.connect('delete-event', gtk.main_quit)
window.show_all()
gtk.main()

[edit] RapidQ

MessageBox("Goodbye, World!", "RapidQ example", 0)

[edit] R

Library: GTK Rather minimalist, but working...

library(RGtk2)   # bindings to Gtk
w <- gtkWindowNew()
l <- gtkLabelNew("Goodbye, World!")
w$add(l)

[edit] REBOL

alert "Goodbye, World!"

[edit] Ruby

Library: GTK

require 'gtk2'
 
window = Gtk::Window.new
window.title = 'Goodbye, World'
window.signal_connect(:delete-event) { Gtk.main_quit }
window.show_all
 
Gtk.main

Library: Ruby/Tk

require 'tk'
root = TkRoot.new("title" => "User Output")
TkLabel.new(root, "text"=>"goodbye world").pack("side"=>'top')
Tk.mainloop

[edit] Scheme

Library: Scheme/PsTk

 
#!r6rs
 
;; PS-TK example: display frame + label
 
(import (rnrs)
(lib pstk main) ; change this to refer to your PS/Tk installation
)
 
(define tk (tk-start))
(tk/wm 'title tk "PS-Tk Example: Label")
 
(let ((label (tk '
create-widget 'label 'text: "Goodbye, world")))
(tk/place label 'height: 20 'width: 50 'x: 10 'y: 20))
 
(tk-event-loop tk)
 

[edit] Smalltalk

MessageBox show: 'Goodbye, world.'

[edit] Tcl

Library: Tk Just output as a label in a window:

pack [label .l -text "Goodbye, World"]

Output as text on a button that exits the current application:

pack [button .b -text "Goodbye, World" -command exit]

Or as a message box:

tk_messageBox -message "Goodbye, World"

[edit] TI-89 BASIC

Dialog
Text "Goodbye, World!"
EndDlog

[edit] Vedit macro language

Displaying the message on status line. The message remains visible until the next keystroke, but macro execution continues.

Statline_Message("Goodbye, World!")

Displaying a dialog box with the message and default OK button:

Dialog_Input_1(1,"`Vedit example`,`Goodbye, World!`")

[edit] UNIX Shell

Using the zenity modal dialogue command (wraps GTK library) available with many distributions of Linux

 
zenity --info --text="Goodbye, World!"
 

[edit] Visual Basic

Sub Main()
MsgBox "Goodbye, World!"
End Sub

[edit] Visual Basic .NET

Works with: Visual Basic version 2005

Module GoodbyeWorld
Sub Main()
Messagebox.Show("Goodbye, World!")
End Sub
End Module

[edit] X86 Assembly

Works with: nasm

This example used the Windows MessageBox function to do the work for us. Windows uses the stdcall calling convention where the caller pushes function parameters onto the stack and the stack has been fixed up when the callee returns.

;;; hellowin.asm
;;;
;;; nasm -fwin32 hellowin.asm
;;; link -subsystem:console -out:hellowin.exe -nodefaultlib -entry:main \
;;; hellowin.obj user32.lib kernel32.lib
 
global _main
extern _MessageBoxA@16
extern _ExitProcess@4
 
MessageBox equ _MessageBoxA@16
ExitProcess equ _ExitProcess@4
 
section .text
_main:
push 0  ; MB_OK
push title  ;
push message  ;
push 0  ;
call MessageBox  ; eax = MessageBox(0,message,title,MB_OK);
push eax  ;
call ExitProcess  ; ExitProcess(eax);
message:
db 'Goodbye, World',0
title:
db 'RosettaCode sample',0
 
Personal tools
Support