GTK+/Development
Contents
Write a simple message dialog app
You can write your own GTK+ 3 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 from AUR
- 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
- Dependency: zenity
hello_world.sh
#!/bin/bash zenity --info --title='Hello world!' --text='This is an example dialog.'
Boo
- Dependency: gtk-sharp-3 (and boo)
- Makedependency: boo
-  Build with: booc hello_world.boo
-  Run with: mono hello_world.exe(orbooi 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: 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 *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));
        return 0;
}
C++
- 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();
}
C#
- 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 ();
	}
}
Cobra
- Dependency: gtk-sharp-3
- Makedependency: cobraAUR[broken link: archived in aur-mirror] from AUR
-  Build with: cobra -c hello_world
-  Run with: mono hello_world.exe
hello_world.cobra
@args -pkg:gtk-sharp-3.0
use Gtk
class HelloWorld
    def main
        Application.init
        hello = MessageDialog(nil, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Hello world!")
        hello.secondaryText = "This is an example dialog."
        hello.run
D
- Dependency: gtkdAUR from AUR
- Makedependency: dmd
-  Build with: dmd hello_world $(pkg-config --cflags --libs gtkd-2)
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: gtk-sharp-3
- Makedependency: fsharpAUR from AUR
-  Build with: fsharpc -r:gtk-sharp.dll -I:/usr/lib/mono/gtk-sharp-3.0/
-  Run with: mono hello_world.exe
hello_world.fs
open Gtk 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
Fortran
- Dependency: gtk-3-fortran-gitAUR[broken link: archived in aur-mirror] from AUR
- Makedependency: gcc-fortran
-  Build with: gfortran hello_world.f90 -o hello_world $(pkg-config --cflags --libs gtk-3-fortran)
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
hello_world.gs
uses 
	Gtk
init
	Gtk.init (ref args)
	var Hello=new MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!")
	Hello.format_secondary_text ("This is an example dialog.")
	Hello.run ()
Go
- Dependency: gtk3
- Makedependency: gotk3-gitAUR[broken link: archived in aur-mirror] from AUR
-  Build with: go build hello_world.go
-  (Or run with: go run hello_world.go)
hello_world.go
package main
import ("github.com/conformal/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 from AUR
-  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(orgroovy -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
- Dependency: gtk3
- Makedependency: haskell-gtk3AUR[broken link: archived in aur-mirror] from AUR
-  Build with: ghc 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
Java
- Dependency: java-gnomeAUR from AUR
- 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
hello_world.js
#!/usr/bin/env gjs
const Gtk = imports.gi.Gtk
Gtk.init(null, null)
var Hello = new Gtk.MessageDialog({type: Gtk.MessageType.INFO,
                                   buttons: Gtk.ButtonsType.OK,
                                   text: "Hello world!",
                                   "secondary-text": "This is an example dialog."})
Hello.run()
Lua
hello_world.lua
#!/usr/bin/env lua
lgi = require 'lgi'
Gtk = lgi.require('Gtk')
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()
Pascal
- Dependency: gtk3
- Makedependencies: fpc, Gtk+3.0 bindings
-  Build with: fpc hello_world
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
- Dependency: perl-gtk3AUR from AUR
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;
Python
- Dependencies: gtk3, python-gobject
hello_world.py
#!/usr/bin/env python
from gi.repository import Gtk
Gtk.init(None)
Hello=Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!")
Hello.format_secondary_text("This is an example dialog.")
Hello.run()
Ruby
- Dependency: ruby-gtk3AUR[broken link: archived in aur-mirror]
hello_world.rb
#!/usr/bin/env ruby
require 'gtk3'
Gtk.init
Hello = Gtk::MessageDialog.new(:type => :info,
                               :buttons_type => :ok,
                               :message => "Hello world!")
Hello.secondary_text = "This is an example dialog."
Hello.run
Scala
- Dependency: java-gnomeAUR from AUR (and scala)
- Makedependency: scala
-  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(orscala -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()
  }
}
Vala
hello_world.vala
using Gtk;
public class HelloWorld {
	static void main (string[] args) {
		Gtk.init (ref args);
		var Hello=new MessageDialog (null, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello world!");
		Hello.format_secondary_text ("This is an example dialog.");
		Hello.run ();
	}
}
Visual Basic .NET
- Dependency: gtk-sharp-3
- Makedependency: mono-basic
-  Build with: vbnc -r:/usr/lib/mono/gtk-sharp-3.0/gio-sharp.dll -r:/usr/lib/mono/gtk-sharp-3.0/glib-sharp.dll -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