GTK/Development

From ArchWiki
< GTK

Write a simple message dialog app

You can write your own GTK message dialog easily in many programming languages through GObject-Introspection or bindings, or you can simply use bash.

The following examples display a simple "Hello world" in a message dialog.

Ada

  • Dependency: gtkadaAUR
  • Makedependency: gcc-ada
  • Build with: gnatmake hello_world `gtkada-config`
hello_world.adb
with Gtk.Main;
with Gtk.Dialog;           use Gtk.Dialog;
with Gtk.Message_Dialog;   use Gtk.Message_Dialog;

procedure hello_world is
  Dialog   : Gtk_Message_Dialog;
  Response : Gtk_Response_Type;
begin
  Gtk.Main.Init;
  Gtk_New (Dialog   => Dialog,
           Parent   => null,
           Flags    => 0,
           The_Type => Message_Info,
           Buttons  => Buttons_OK,
           Message  => "Hello world!");
  Format_Secondary_Markup (Dialog, "This is an example dialog.");
  Response := Run (Dialog);
end hello_world;

Bash

hello_world.sh
#!/bin/bash
zenity --info --title='Hello world!' --text='This is an example dialog.'

BASIC

hello_world.bas
#include "Gir/Gtk-3.0.bi"
Dim As GtkWidget Ptr hello
gtk_init (cast(gint ptr, @__fb_argc__), @__fb_argv__)
hello = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello world!")
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (hello), "This is an example dialog.")
gtk_dialog_run (GTK_DIALOG (hello))

Boo

  • Dependency: gtk-sharp-3 (and booAUR[broken link: package not found])
  • Makedependency: booAUR[broken link: package not found]
  • Build with: booc hello_world.boo
  • Run with: mono hello_world.exe (or booi hello_world.boo)
hello_world.boo
import Gtk from "gtk-sharp"
Application.Init()
Hello = MessageDialog(null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!")
Hello.SecondaryText = "This is an example dialog."
Hello.Run()

C

  • Dependency: gtk4
  • Build with: gcc -o hello_world $(pkg-config --cflags --libs gtk4) hello_world.c
hello_world.c
#include <gtk/gtk.h>

static void activate (GtkApplication *app, gpointer user_data) {
  GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello world!");
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "This is an example dialog.");
  gtk_window_set_application (GTK_WINDOW (dialog), app);
  g_signal_connect (dialog, "response", G_CALLBACK (gtk_window_destroy), dialog);
  gtk_window_present (GTK_WINDOW (dialog));
}

int main (int argc, char **argv) {
  GtkApplication *app = gtk_application_new (NULL, G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  return g_application_run (G_APPLICATION (app), argc, argv);
}
  • Dependency: gtk3
  • Build with: gcc -o hello_world $(pkg-config --cflags --libs gtk+-3.0) hello_world.c
hello_world.c
#include <gtk/gtk.h>

int main (int argc, char *argv[]) {
  gtk_init (&argc, &argv);
  GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "dialog world!");
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "This is an example dialog.");
  gtk_dialog_run(GTK_DIALOG (dialog));
  return 0;
}

C++

  • Dependency: gtkmm-4.0
  • Build with: g++ -o hello_world $(pkg-config --cflags --libs gtkmm-4.0) hello_world.cc
hello_world.cc
#include <gtkmm.h>

class MyDialog : public Gtk::MessageDialog {
  public:
    MyDialog() : MessageDialog("Hello world!", false, Gtk::MessageType::INFO, Gtk::ButtonsType::OK, false) {
      set_secondary_text("This is an example dialog.");
      signal_response().connect(sigc::hide(sigc::mem_fun(*this, &Gtk::Window::destroy)));
    };
};

int main(int argc, char* argv[]) {
  auto app = Gtk::Application::create();
  return app->make_window_and_run<MyDialog>(argc, argv);
}
  • Dependency: gtkmm3
  • Build with: g++ -o hello_world $(pkg-config --cflags --libs gtkmm-3.0) hello_world.cc
hello_world.cc
#include <gtkmm/main.h>
#include <gtkmm/messagedialog.h>
int main(int argc, char *argv[]) {
	Gtk::Main kit(argc, argv);
	Gtk::MessageDialog Hello("Hello world!", false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK);
	Hello.set_secondary_text("This is an example dialog.");
	Hello.run();
	return 0;
}

See also:

C#

.NET

  • Dependencies: dotnet-runtime, gtk3
  • Makedependency: dotnet-sdk
  • Build with: dotnet build
  • Run with: bin/Debug/net7.0/HelloWorld (or dotnet run)
HelloWorld.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="GtkSharp" Version="3.24.24.38" />
  </ItemGroup>
</Project>
HelloWorld.cs
using Gtk;
public class HelloWorld {
	static void Main() {
		Application.Init ();
		MessageDialog Hello = new MessageDialog (null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!");
		Hello.SecondaryText="This is an example dialog.";
		Hello.Run ();
	}
}

Mono

  • Dependency: gtk-sharp-3
  • Build with: mcs -pkg:gtk-sharp-3.0 hello_world.cs
  • Run with: mono hello_world.exe
hello_world.cs
using Gtk;
public class HelloWorld {
	static void Main() {
		Application.Init ();
		MessageDialog Hello = new MessageDialog (null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!");
		Hello.SecondaryText="This is an example dialog.";
		Hello.Run ();
	}
}

Clojure

project.clj
(defproject hello-world "0.1.0-SNAPSHOT"
  :main hello-world.core
  :dependencies [[org.clojure/clojure "1.10.3"]]
  :resource-paths ["/usr/share/java/gtk.jar"])
src/hello_world/core.clj
(ns hello-world.core
  (:import [org.gnome.gtk Gtk InfoMessageDialog]))

(defonce gtk-init (Gtk/init (make-array String 0)))
  
(defn -main []
  (doto (InfoMessageDialog. nil "Hello world!" "This is an example dialog.").run))

Crystal

hello_world.cr
require "gobject/gtk/autorun"

dialog = Gtk::MessageDialog.new text: "Hello world!", message_type: :info, buttons: :ok, secondary_text: "This is an example dialog."
dialog.on_response do
  Gtk.main_quit
end
dialog.show

D

  • Dependency: gtkd
  • Makedependency: ldc
  • Build with: ldc hello_world $(pkg-config --cflags --libs gtkd-3)
hello_world.d
import gtk.Main;
import gtk.MessageDialog;

void main(string[] args)
{
	Main.init(args);
	MessageDialog dialog = new MessageDialog(null, GtkDialogFlags.MODAL, MessageType.INFO, ButtonsType.OK, "Hello world!");
	dialog.run();
}

F#

  • Dependency: dotnet-sdk
  • Run with: dotnet fsi hello_world.fsx
hello_world.fsx
#r "nuget: GtkSharp"
open Gtk

let main () =
  Application.Init()
  let Hello = new MessageDialog(null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!")
  Hello.SecondaryText <- "This is an example dialog."
  Hello.Run() |> ignore

main ()

Fortran

  • Dependency: gtk-3-fortran-gitAUR
  • Makedependency: gcc-fortran
  • Build with: gfortran -o hello_world $(pkg-config --cflags --libs gtk-3-fortran) hello_world.f90
hello_world.f90
program hello_world
  use gtk_hl
  use gtk, only: gtk_init

  integer(c_int) :: resp
  character(40), dimension(2) :: msg

  call gtk_init()
  msg(1) ="Hello world!"
  msg(2) = "This is an example dialog."
  resp = hl_gtk_message_dialog_show(msg, GTK_BUTTONS_OK, type=GTK_MESSAGE_INFO)
end program hello_world

Genie

  • Dependency: gtk4
  • Makedependency: vala
  • Build with: valac --pkg gtk4 hello_world.gs
hello_world.gs
class Application
	application: Gtk.Application
	dialog: Gtk.MessageDialog

	construct ()
		application = new Gtk.Application (null, GLib.ApplicationFlags.FLAGS_NONE)
		application.activate.connect (on_activate)
		application.run ()
	
	def on_activate ()
		dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!")
		dialog.application = application
		dialog.format_secondary_text ("This is an example dialog.")
		dialog.response.connect (on_response)
		dialog.present ()

	def on_response ()
		dialog.destroy ()

init
	new Application
  • Dependency: gtk3
  • Makedependency: vala
  • Build with: valac --pkg gtk+-3.0 hello_world.gs
hello_world.gs
init
	Gtk.init (ref args)
	var dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!")
	dialog.format_secondary_text ("This is an example dialog.")
	dialog.run ()

Go

  • Dependency: gtk3
  • Makedependency: gotk3-gitAUR or go get github.com/gotk3/gotk3/gtk
  • Build with: go build hello_world.go
  • (Or run with: go run hello_world.go)
hello_world.go
package main
import ("github.com/gotk3/gotk3/gtk")

func main() {
	gtk.Init(nil)
	dialog := gtk.MessageDialogNew(nil, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Hello world!")
	dialog.FormatSecondaryText("This is an example notification.")
	dialog.Run()
}

Groovy

  • Dependencies: groovy, java-gnomeAUR
  • Build with: groovyc -cp /usr/share/java/gtk.jar HelloWorld.groovy && jar cfe HelloWorld.jar HelloWorld HelloWorld.class
  • Run with: java -cp /usr/share/groovy/embeddable/groovy-all.jar:/usr/share/java/gtk.jar:HelloWorld.jar HelloWorld or groovy -cp /usr/share/java/gtk.jar HelloWorld.groovy
HelloWorld.groovy
import org.gnome.gtk.*
Gtk.init()
def Hello = new InfoMessageDialog(null, "Hello world!", "This is an example dialog.")
Hello.run()

Haskell

hello_world.hs
{-# LANGUAGE OverloadedStrings, OverloadedLabels, ImplicitParams #-}
import Control.Monad (void)
import qualified GI.Gtk as Gtk
import Data.GI.Base

activate app = do
  dialog <- new Gtk.MessageDialog [ #application := app,
                                    #buttons := Gtk.ButtonsTypeOk,
                                    #title := "Hello world!",
                                    #text := "This is an example dialog.",
                                    #messageType := Gtk.MessageTypeInfo ]
  on dialog #response $ \_ -> #destroy dialog
  #show dialog

main = do
  app <- new Gtk.Application [On #activate (activate ?self)]
  #run app Nothing
hello_world.hs
{-# LANGUAGE OverloadedStrings, OverloadedLabels, ImplicitParams #-}
import Control.Monad (void)
import qualified GI.Gtk as Gtk
import Data.GI.Base

main = do
  _ <- Gtk.init Nothing
  dialog <- new Gtk.MessageDialog [ #buttons := Gtk.ButtonsTypeOk,
                                    #title := "Hello world!",
                                    #text := "This is an example dialog.",
                                    #messageType := Gtk.MessageTypeInfo ]
  #run dialog
  • Dependency: haskell-gtk3
  • Makedependency: ghc
  • Build with: ghc -dynamic hello_world
hello_world.hs
import Graphics.UI.Gtk
main = do
         initGUI
         dialog <- messageDialogNew Nothing [DialogModal] MessageInfo ButtonsOk "Hello world!"
         messageDialogSetSecondaryText dialog "This is an example dialog."
         _res <- dialogRun dialog
         return 0

IronPython

hello_world.py
import clr
clr.AddReference('gtk-sharp')
import Gtk
Gtk.Application.Init()
Hello = Gtk.MessageDialog (None, Gtk.DialogFlags.Modal, Gtk.MessageType.Info, Gtk.ButtonsType.Ok, "Hello world!")
Hello.SecondaryText="This is an example dialog."
Hello.Run()

Java

  • Dependency: java-gnomeAUR
  • Makedependency: java-environment
  • Build with: javac -cp /usr/share/java/gtk.jar HelloWorld.java && jar cfe HelloWorld.jar HelloWorld HelloWorld.class
  • Run with: java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld
HelloWorld.java
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Dialog;
import org.gnome.gtk.InfoMessageDialog;

public class HelloWorld {
    public static void main(String[] args) {
        Gtk.init(args);
        Dialog Hello = new InfoMessageDialog(null, "Hello world!", "This is an example dialog.");
        Hello.run();
    }
}

JavaScript

GJS

hello_world.js
#!/usr/bin/env -S gjs -m
import Gtk from 'gi://Gtk?version=4.0'

const application = new Gtk.Application()

application.connect("activate", () => {
  const dialog = new Gtk.MessageDialog({
    application,
    message_type: Gtk.MessageType.INFO,
    buttons: Gtk.ButtonsType.OK,
    text: "Hello world!",
    "secondary-text": "This is an example dialog."
  })
  dialog.connect("response", () => {
    dialog.destroy()
  })
  dialog.present()
})

application.run(null)
hello_world.js
#!/usr/bin/env gjs
imports.gi.versions.Gtk = "3.0"
const Gtk = imports.gi.Gtk

Gtk.init(null)

const Hello = new Gtk.MessageDialog({
  type: Gtk.MessageType.INFO,
  buttons: Gtk.ButtonsType.OK,
  text: "Hello world!",
  "secondary-text": "This is an example dialog."
})

Hello.run()

See also:

  • DevDocs for GJS — API documentation browser for the GNOME JavaScript platform
  • GJS Guide — A Guide to JavaScript for GNOME
  • GTK4 + GJS — Learn to build a GTK4 application with GJS

Node.js

Using node-gtk.

  • Dependencies: gtk4, nodejs
  • Makedependency: npm
  • Build with: npm install
package.json
{
  "dependencies": {
    "node-gtk": "^0.11.0"
  }
}
hello_world.js
#!/usr/bin/env node
const gi = require('node-gtk')
const Gtk = gi.require('Gtk', '4.0')

const application = new Gtk.Application()

application.connect("activate", () => {
  const dialog = new Gtk.MessageDialog({
    application,
    message_type: Gtk.MessageType.INFO,
    buttons: Gtk.ButtonsType.OK,
    text: "Hello world!",
    "secondary-text": "This is an example dialog."
  })
  dialog.connect("response", () => {
    dialog.destroy()
  })
  dialog.present()
})

application.run(null)
  • Dependencies: gtk3, nodejs,
  • Makedependency: npm
  • Build with: npm install
package.json
{
  "dependencies": {
    "node-gtk": "^0.11.0"
  }
}
hello_world.js
#!/usr/bin/env node
const gi = require('node-gtk')
const Gtk = gi.require('Gtk', '3.0')

Gtk.init(null)

const Hello = new Gtk.MessageDialog({
  type: Gtk.MessageType.INFO,
  buttons: Gtk.ButtonsType.OK,
  text: "Hello world!",
  "secondary-text": "This is an example dialog."
})

Hello.run()

JRuby

  • Dependencies: java-gnomeAUR, jruby
  • Build with: jrubyc hello_world.rb && jar cfe hello_world.jar hello_world hello_world.class
  • Run with: java -cp /opt/jruby/lib/jruby.jar:hello_world.jar hello_world or jruby hello_world.rb
hello_world.rb
require '/usr/share/java/gtk.jar'
import Java::OrgGnomeGtk::Gtk
import Java::OrgGnomeGtk::Dialog
import Java::OrgGnomeGtk::InfoMessageDialog

Gtk.init(nil)
Hello = InfoMessageDialog.new(nil, "Hello world!", "This is an example dialog.")
Hello.run

Julia

  • Dependencies: julia, Gtk.jl
  • Run with: julia hello_world.jl
hello_world.jl
using Gtk
info_dialog("Hello world!\n\nThis is an example dialog.")

Jython

  • Dependencies: java-gnomeAUR, jython
  • Run with: jython -Dpython.path=/usr/share/java/gtk.jar hello_world.py
hello_world.py
from org.gnome.gtk import Gtk, InfoMessageDialog
Gtk.init(None)
Hello=InfoMessageDialog(None, "Hello world!", "This is an example dialog.")
Hello.run()

Kotlin

  • Dependency: java-gnomeAUR
  • Makedependency: kotlin
  • Build with: kotlinc -cp /usr/share/java/gtk.jar HelloWorld.kt -include-runtime -d HelloWorld.jar
  • Run with: java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorldKt
HelloWorld.kt
import org.gnome.gtk.*

fun main(args : Array<String>) {
        Gtk.init(args);
        val Hello = InfoMessageDialog(null, "Hello world!", "This is an example dialog.");
        Hello.run();
}

Lua

hello_world.lua
#!/usr/bin/env lua
lgi = require 'lgi'
Gtk = lgi.require('Gtk', '3.0')
Gtk.init()
Hello=Gtk.MessageDialog {message_type = Gtk.MessageType.INFO,
                         buttons = Gtk.ButtonsType.OK,
                         text = "Hello world!",
                         secondary_text = "This is an example dialog."}
Hello:run()

Nemerle

  • Dependency: gtk-sharp-3
  • Makedependency: nemerleAUR
  • Build with: ncc -pkg:gtk-sharp-3.0 -out:hello_world.exe hello_world.n
  • Run with: mono hello_world.exe
hello_world.n
using Gtk;
public class HelloWorld {
	static Main() : void {
		Application.Init ();
		def Hello = MessageDialog (null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!");
		Hello.SecondaryText = "This is an example dialog.";
		_ = Hello.Run ();
	}
}

OCaml

  • Dependency: gtk3
  • Makedependency: lablgtk3
  • Build with: ocamlopt -I +lablgtk3 lablgtk3.cmxa hello_world.ml -o hello_world
hello_world.ml
let main () =
  GMain.init ();
  let hello = GWindow.message_dialog ~message:"Hello world!" ~message_type:`INFO ~buttons:GWindow.Buttons.ok () in
    hello#set_secondary_text("This is an example dialog.");
    hello#run ()

let _ = main ()

Pascal

hello_world.pas
program	hello_world;
uses	Math, Gtk3;
var	dialog: PGtkWidget;
begin
	SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);
	gtk_init(@argc, @argv);
	dialog := gtk_message_dialog_new(nil, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, '%s', ['Hello world!']);
	gtk_message_dialog_format_secondary_text(PGtkMessageDialog(dialog), '%s', ['This is an example dialog.']);
	gtk_dialog_run(PGtkDialog(dialog));
end.

Perl

hello_world.pl
#!/usr/bin/env perl
use Glib::Object::Introspection;
Glib::Object::Introspection->setup(basename => 'Gio', version => '2.0', package => 'Glib::IO');
Glib::Object::Introspection->setup(basename => 'Gtk', version => '4.0', package => 'Gtk4');

my $application = Glib::Object::new('Gtk4::Application');

$application->signal_connect(activate => sub {
  my $dialog = Glib::Object::new ('Gtk4::MessageDialog',
                                  message_type => 'info',
                                  buttons => 'ok',
                                  text => "Hello world!",
                                  secondary_text => 'This is an example dialog.');
  $dialog->set_application($application);
  $dialog->signal_connect(response => sub { $dialog->close(); });
  $dialog->present();
});

$application->run();
hello_world.pl
#!/usr/bin/env perl
use Gtk3 -init;
my $hello = Gtk3::MessageDialog->new (undef, 'modal', 'info', 'ok', "Hello world!");
$hello->set ('secondary-text' => 'This is an example dialog.');
$hello->run;

Prolog

  • Dependency: plgiAUR[broken link: package not found]
  • Run with: swipl hello_world.pl
hello_world.pl
:- use_module(library(plgi)).
:- plgi_use_namespace('Gtk').

main :-
	g_object_new('GtkMessageDialog',
	             ['message-type'='GTK_MESSAGE_INFO',
	              'buttons'='GTK_BUTTONS_OK',
	              'text'='Hello world!',
	              'secondary-text'='This is an example dialog.'],
	             Dialog),
	gtk_dialog_run(Dialog, _),
	halt.
:- main.

Python

hello_world.py
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

def on_activate(application):
    dialog = Gtk.MessageDialog(application=application,
                               message_type=Gtk.MessageType.INFO,
                               buttons=Gtk.ButtonsType.OK,
                               text="Hello world!",
                               secondary_text="This is an example dialog.")
    dialog.connect("response", lambda d, self: d.destroy())
    dialog.present()

application = Gtk.Application()
application.connect('activate', on_activate)
application.run(None)
hello_world.py
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
Gtk.init(None)
Hello = Gtk.MessageDialog(message_type=Gtk.MessageType.INFO,
                          buttons=Gtk.ButtonsType.OK,
                          text="Hello world!",
                          secondary_text="This is an example dialog.")
Hello.run()

See also:

Ruby

hello_world.rb
#!/usr/bin/env ruby
require 'gtk4'

application = Gtk::Application.new()

application.signal_connect "activate" do
  dialog = Gtk::MessageDialog.new(:type => :info,
                                  :buttons_type => :ok,
                                  :message => "Hello world!")
  dialog.secondary_text = "This is an example dialog."
  dialog.application = application
  dialog.signal_connect "response" do
    dialog.destroy
  end
  dialog.present
end

application.run
hello_world.rb
#!/usr/bin/env ruby
require 'gtk3'
Gtk.init
dialog = Gtk::MessageDialog.new(:type => :info,
                               :buttons_type => :ok,
                               :message => "Hello world!")
dialog.secondary_text = "This is an example dialog."
dialog.run
hello_world.rb
#!/usr/bin/env ruby
require 'gir_ffi-gtk3'
Gtk.init
dialog = Gtk::MessageDialog.new nil, :modal, :info, :ok, "Hello world!"
dialog.secondary_text = "This is an example dialog."
dialog.run

Rust

Using gtk-rs.

  • Dependency: gtk4
  • Makedependency: rust
  • Build with: cargo build
  • Run with: target/debug/hello_world or cargo run
Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"

[dependencies]
gtk4 = "^0"
src/main.rs
extern crate gtk4;
use gtk4::prelude::*;

fn main() {
    let app = gtk4::Application::builder()
        .build();
    app.connect_activate(|application| {
        let dialog = gtk4::MessageDialog::builder()
            .application(application)
            .message_type(gtk4::MessageType::Info)
            .buttons(gtk4::ButtonsType::Ok)
            .text("Hello world!")
            .secondary_text("This is an example dialog.")
            .build();
        dialog.connect_response(|dialog, _| dialog.destroy());
        dialog.present();
    });
    app.run();
}
  • Dependency: gtk3
  • Makedependency: rust
  • Build with: cargo build
  • Run with: target/debug/hello_world or cargo run
Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"

[dependencies]
gtk = "^0"
src/main.rs
extern crate gtk;
use gtk::prelude::*;

fn main() {
    let _ = gtk::init();
    let dialog = gtk::MessageDialog::builder()
        .message_type(gtk::MessageType::Info)
        .buttons(gtk::ButtonsType::Ok)
        .text("Hello world!")
        .secondary_text("This is an example dialog.")
        .build();
    dialog.run();
}

See also:

Scala

  • Dependency: java-gnomeAUR (and scalaAUR)
  • Makedependency: scalaAUR
  • Build with: scalac -cp /usr/share/java/gtk.jar -d HelloWorld.jar HelloWorld.scala
  • Run with: java -cp /usr/share/java/gtk.jar:HelloWorld.jar HelloWorld (or scala -cp /usr/share/java/gtk.jar HelloWorld.scala)
HelloWorld.scala
import org.gnome.gtk._
 
object HelloWorld {
  def main(args: Array[String]) {
    Gtk.init(args)
    var hello = new InfoMessageDialog(null, "Hello world!", "This is an example dialog.")
    hello.run()
  }
}

Scheme

hello-world.scm
#!/usr/bin/env guile
!#
(use-modules (gi)
             (gi util))
(push-duplicate-handler! 'merge-generics)
(use-typelibs ("Gio" "2.0")
              ("Gtk" "4.0"))

(define (activate app)
  (let ((dialog (make <GtkMessageDialog> #:application app #:message-type 'info #:buttons 'ok #:text "Hello world!" #:secondary-text "This is an example dialog.")))
    (connect dialog response (lambda (a, b) (destroy dialog)))
    (present dialog)))

(let ((app (make <GtkApplication>)))
  (connect app application:activate activate)
  (run app '()))
hello-world.scm
#!/usr/bin/env guile
!#
(use-modules (gi))
(use-typelibs ("Gtk" "3.0"))
(init!)
(let ((dialog (make <GtkMessageDialog> #:message-type 'info #:buttons 'ok #:text "Hello world!" #:secondary-text "This is an example dialog.")))
(run dialog))

Vala

  • Dependency: gtk4
  • Makedependency: vala
  • Build with: valac --pkg gtk4 hello_world.vala
hello_world.vala
int main (string[] argv) {
	var application = new Gtk.Application (null, GLib.ApplicationFlags.FLAGS_NONE);

	application.activate.connect (() => {
		var dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!");
		dialog.application = application;
		dialog.format_secondary_text ("This is an example dialog.");
		dialog.response.connect (() => {
			dialog.destroy ();
		});
		dialog.present ();
	});

	return application.run (argv);
}
  • Dependency: gtk3
  • Makedependency: vala
  • Build with: valac --pkg gtk+-3.0 hello_world.vala
hello_world.vala
static void main (string[] args) {
	Gtk.init (ref args);
	var dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!");
	dialog.format_secondary_text ("This is an example dialog.");
	dialog.run ();
}

See also:

Visual Basic

  • Dependency: gtk-sharp-3
  • Makedependency: mono-basicAUR
  • Build with: vbnc -r:/usr/lib/mono/gtk-sharp-3.0/gtk-sharp.dll hello_world.vb
  • Run with: mono hello_world.exe
hello_world.vb
Imports Gtk
Public Class Hello
	Inherits MessageDialog
	Public Sub New
		MyBase.New(Me, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!")
		Me.SecondaryText = "This is an example dialog."
	End Sub
	Public Shared Sub Main
		Application.Init
		Dim Dialog As New Hello
		Dialog.Run
	End Sub
End Class

See also