programmazione:d:gtkd:hello_world
Hello world in GTK-D
Autore: Fabio Di Matteo
Ultima revisione: 03/07/2025 12:56
Funziona ancora , ma io userei Hello world in gtk4 e linguaggio D usando Gid
Un hello world che mostra le basi di una finestra GTK-D .
Trovo la documentazione un pochino carente al momento. Mi sono aiutato guardando gli esempi dentro la directory “demos” del repository ufficiale GtkD . In più dato che ho usato dub package manager ho trovato utile dare una sbirciata direttamente al codice sorgente di GTK-D . I file si trovano qui:
ls $HOME/.dub/packages/gtk-d/3.11.0/gtk-d/generated/gtkd/gtk
Altra risorsa utile: gid-gtk4-examples
Una semplice finestra con un bottone
import gtk.MainWindow; import gtk.Label; import gtk.Main; import gtk.Button; import std.stdio; void onClicked(Button b) { writeln("Cliccato."); } void main(string[] args) { Main.init(args); MainWindow win = new MainWindow("Hello World"); win.setDefaultSize(200, 100); auto mybtn=new Button(); mybtn.setLabel("Clicca"); mybtn.addOnClicked(delegate void(Button b){ onClicked(b); b.setLabel("Cliccato."); }); win.add(mybtn); win.showAll(); Main.run(); }
dub.json
{ "authors": [ "fabio" ], "copyright": "Copyright © 2025, fabio", "dependencies": { "gtk-d": "~>3.11.0" }, "description": "A minimal D application.", "license": "GPL-2.0-only", "name": "gtk3-builder" }
Uso di Gtk Builder e Glade
app.d
import gio.Application: GioApplication = Application; import gtk.Application; import gtk.ApplicationWindow; import gtk.Builder; import gtk.Button; import gtk.Entry; import gtk.Label; import std.stdio; import core.stdc.stdlib; int main(string[] args) { auto application = new Application("org.gtkd.demo.builder.builderTest", GApplicationFlags.FLAGS_NONE); void initGui(GioApplication a) { auto builder = new Builder(); if( ! builder.addFromFile("gui.ui") ) { writeln("Oops, could not create Glade object, check your glade file ;)"); exit(1); } auto mywin = cast(ApplicationWindow)builder.getObject("mywin"); if (mywin is null) { writeln("mywin è null"); } mywin.setApplication(application); mywin.setTitle("This is a glade application window"); auto mylabel = cast(Label)builder.getObject("mylabel"); auto myentry = cast(Entry)builder.getObject("myentry"); auto mybutton = cast(Button)builder.getObject("mybutton"); mybutton.addOnClicked(delegate void(Button b){ string t= myentry.getText(); if (t!="") { mylabel.setLabel(t); }else{ mylabel.setLabel("Inserisci il testo nella entry e clicca sul bottone."); } }); mywin.showAll(); } application.addOnActivate(&initGui); return application.run(args); }
gui.ui
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.40.0 --> <interface> <requires lib="gtk+" version="3.24"/> <object class="GtkApplicationWindow" id="mywin"> <property name="can-focus">False</property> <child> <object class="GtkBox"> <property name="visible">True</property> <property name="can-focus">False</property> <property name="orientation">vertical</property> <child> <object class="GtkLabel" id="mylabel"> <property name="visible">True</property> <property name="can-focus">False</property> <property name="label" translatable="yes">label</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkEntry" id="myentry"> <property name="visible">True</property> <property name="can-focus">True</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkButton" id="mybutton"> <property name="label" translatable="yes">button</property> <property name="visible">True</property> <property name="can-focus">True</property> <property name="receives-default">True</property> </object> <packing> <property name="expand">False</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> </object> </child> </object> </interface>
programmazione/d/gtkd/hello_world.txt · Ultima modifica: 10/07/2025 22:07 da Fabio Di Matteo