<?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="hello-world.py" xml:lang="sv">

  <info>
  <title type="text">Hej världen (Python)</title>
    <link type="guide" xref="py#tutorial" group="#first"/>

    <revision version="0.1" date="2013-06-17" status="review"/>

    <credit type="author copyright">
      <name>Susanna Huhtanen</name>
      <email its:translate="no">ihmis.suski@gmail.com</email>
      <years>2012</years>
    </credit>
    <credit type="editor">
      <name>Tiffany Antopolski</name>
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
    </credit>

    <desc>Ett grundläggande ”Hej världen”-program</desc>
  
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
      <mal:name>Sebastian Rasmussen</mal:name>
      <mal:email>sebras@gmail.com</mal:email>
      <mal:years>2019</mal:years>
    </mal:credit>
  
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
      <mal:name>Anders Jonsson</mal:name>
      <mal:email>anders.jonsson@norsjovallen.se</mal:email>
      <mal:years>2021</mal:years>
    </mal:credit>
  </info>

  <title>Hur du bygger, installerar och skapar en <file>tar.xz</file> för ett ”Hej världen”-program</title>
    <media type="image" mime="image/png" style="floatend" src="media/hello-world.png"/>
    <synopsis>
      <p>Denna handledning kommer demonstrera hur du:</p>
      <list style="numbered">
        <item><p>skapa ett litet ”Hej världen”-program med Python och GTK+</p></item>
        <item><p>gör <file>.desktop</file>-filen</p></item>
        <item><p>konfigurerar byggsystemet</p></item>
      </list>
    </synopsis>



  <links type="section"/>

  <section id="HelloWorld"><title>Skapa programmet</title>

    <links type="section"/>

    <section id="imports"><title>Bibliotek att importera</title>
      <code mime="text/x-python">from gi.repository import Gtk
import sys</code>
      <p>För att vårt skript ska fungera med GNOME behöver vi importera GNOME-bibliotek via GObject Introspection. Här importerar vi språkbindningarna och GTK+, biblioteket som innehåller de grafiska komponenterna som används för att göra GNOME-program.</p>
    </section>

    <section id="mainwindow"><title>Creating the main window for the application</title>
      <code mime="text/x-python">class MyWindow(Gtk.ApplicationWindow):

    # konstruktor för ett Gtk.ApplicationWindow
    def __init__(self, app):
        Gtk.Window.__init__(self, title="Hej världen!", application=app)
        self.set_default_size(200, 100)

class MyApplication(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)</code>

    <p>Gtk.Application initierar GTK+. Det ansluter också knappen <gui>x</gui> som genereras automatiskt tillsammans med fönstret till signalen ”destroy”.</p>
    <p>Vi kan börja med att bygga vårt första fönster. Vi gör detta genom att skapa en klass med namnet <var>MyWindow</var> och tilldela den ett Gtk.ApplicationWindow.</p>
    <p>Vi ger fönstret en egenskap med namnet <var>title</var>. Titeln kan vara vilken sträng som du vill. För att vara på den säkra sidan är det bäst att hålla sig till UTF-8-kodning.</p>
    <p>Nu har vi ett fönster som har en titel och en fungerande ”stäng”-knapp. Låt oss lägga till den faktiska texten ”Hej världen”.</p>
    </section>

    <section id="label"><title>Etikett för fönstret</title>
      <code mime="text/x-python"># Lägg till en etikettkomponent till ditt fönster

        # skapa en etikett
        label = Gtk.Label()

        # ställ in etikettens text
        label.set_text("Hej GNOME!")

        # lägg till etiketten till fönstret
        self.add(label)</code>

      <p>En textetikett är en av GTK+-komponenterna vi kan använda eftersom vi har importerat GTK+-biblioteket. För att använda den skapar vi en ny variabel med namnet <var>label</var>, och ställer in texten som etiketten ska innehålla. Slutligen skapar vi och kör programmet:</p>

      <code mime="text/x-python">#kör programmet

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)</code>

      <p>Gtk.ApplicationWindow kan endast innehålla en komponent åt gången. För att konstruera mer avancerade program behöver du skapa en behållarkomponent som Gtk.Grid inuti fönstret, och sedan lägga till alla andra komponenter till den.</p>
   </section>


    <section id="py"><title>hello-world.py</title>
      <p>Den fullständiga filen:</p>
      <code mime="text/x-python" style="numbered">from gi.repository import Gtk
import sys


class MyWindow(Gtk.ApplicationWindow):
    # konstruktor för ett Gtk.ApplicationWindow

    def __init__(self, app):
        Gtk.Window.__init__(self, title="Välkommen till GNOME", application=app)
        self.set_default_size(200, 100)

        # skapa en etikett
        label = Gtk.Label()
        # ställ in etikettens text
        label.set_text("Hej GNOME!")
        # lägg till etiketten till fönstret
        self.add(label)


class MyApplication(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
</code>
    </section>

    <section id="terminal"><title>Köra programmet från terminalen</title>
      <p>För att köra detta program, spara det först som hello-world.py. Öppna sedan Terminal, gå till mappen där ditt program lagrats och kör:</p>
      <screen><output style="prompt">$ </output><input>python hello-world.py</input></screen>
    </section>
  </section>

  <section id="desktop.in"><title><file>.desktop.in</file>-filen</title>
      <p>Running applications from the Terminal is useful at the beginning of the application making process. To have fully working <link href="https://developer.gnome.org/integration-guide/stable/mime.html.en">application integration</link> in GNOME 3 requires a desktop launcher. For this you need to create a  <file>.desktop</file> file. The <file>.desktop</file> file describes the application name, the used icon and various integration bits. A deeper insight into the <file>.desktop</file> file can be found <link href="http://developer.gnome.org/desktop-entry-spec/">here</link>. The <file>.desktop.in</file> file will create the <file>.desktop</file>.</p>

    <p>Exemplet visar dig minimikraven för en <code>.desktop.in</code>-fil.</p>
    <code mime="text/desktop" style="numbered">[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Hello World
Comment=Säg hej
Exec=@prefix@/bin/hello-world
Icon=application-default-icon
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;Utility;
</code>

    <p>Låt oss nu gå igenom några delar av <code>.desktop.in</code>-filen.</p>
    <terms>
      <item><title>Name</title><p>Programmets namn.</p></item>
      <item><title>Comment</title><p>En kort beskrivning av programmet.</p></item>
      <item><title>Exec</title><p>Anger ett kommando att köra när du väljer programmet från menyn. I detta exempel säger exec bara till var filen <file>hello-world</file> kan hittas, så tar filen hand om resten.</p></item>
      <item><title>Terminal</title><p>Anger huruvida kommandot i Exec-nyckeln körs i ett terminalfönster.</p></item>
    </terms>

    <p>För att lägga ditt program i rätt kategori behöver du lägga till de nödvändiga kategorierna till raden Categories. Mer information om det olika kategorierna kan hittas i <link href="http://standards.freedesktop.org/menu-spec/latest/apa.html">menyspecifikationen</link>.</p>
    <p>I detta exempel använder vi en befintlig ikon. För en anpassad ikon behöver du ha en .svg-fil av din ikon, lagrad i <file>/usr/share/icons/hicolor/scalable/apps</file>. Skriv namnet på din ikonfil i .desktop.in-filen, på rad 7. Mer information om ikoner finns i: <link href="https://wiki.gnome.org/Initiatives/GnomeGoals/AppIcon">Installing Icons for Themes</link> och <link href="http://freedesktop.org/wiki/Specifications/icon-theme-spec">på freedesktop.org: Specifications/icon-theme-spec</link>.</p>
  </section>

  <section id="autotools"><title>Byggsystemet</title>
    <p>För att verkligen göra ditt program till en del av GNOME 3-systemet behöver du installera det med hjälp av autotools. Detta autotools-bygge kommer installera alla nödvändiga filer till rätt plats.</p>
    <p>För detta behöver du ha följande filer:</p>
    <links type="section"/>

      <section id="autogen"><title>autogen.sh</title>
        <code mime="application/x-shellscript" style="numbered">#!/bin/sh

set -e

test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.

olddir=`pwd`
cd "$srcdir"

# Detta kommer köra autoconf, automake o.s.v. åt oss
autoreconf --force --install

cd "$olddir"

if test -z "$NOCONFIGURE"; then
  "$srcdir"/configure "$@"
fi
</code>

      <p>Efter att filen <file>autogen.sh</file> är klar och sparad, kör:</p>
      <screen><output style="prompt">$ </output><input>chmod +x autogen.sh</input></screen>
    </section>


    <section id="makefile"><title>Makefile.am</title>
      <code mime="application/x-shellscript" style="numbered"># The actual runnable program is set to the SCRIPTS primitive.
# # Prefix bin_ tells where to copy this
bin_SCRIPTS = hello-world.py
# # List of files to be distributed
EXTRA_DIST=  \
	$(bin_SCRIPTS)
#
#     # The desktop files
desktopdir = $(datadir)/applications
desktop_DATA = \
	hello-world.desktop
</code>
    </section>


    <section id="configure"><title>configure.ac</title>
      <code mime="application/x-shellscript" style="numbered"># This file is processed by autoconf to create a configure script
AC_INIT([Hello World], 1.0)
AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip])
AC_CONFIG_FILES([Makefile hello-world.desktop])
AC_OUTPUT
</code>
    </section>


    <section id="readme"><title>README</title>
       <p>Information som användare bör läsa först. Denna fil kan vara tom.</p>

       <p>När du har filerna <file>hello-world</file>, <file>hello-world.desktop.in</file>, <file>Makefile.am</file>, <file>configure.ac</file> och <file>autogen.sh</file> med rätt information och rättigheter, kan filen <file>README</file> innehålla följande instruktioner:</p>
      <code mime="text/readme" style="numbered">To build and install this program:

./autogen.sh --prefix=/home/your_username/.local
make install

-------------
Running the first line above creates the following files:

aclocal.m4
autom4te.cache
config.log
config.status
configure
hello-world.desktop
install-sh
missing
Makefile.in
Makefile

Running "make install", installs the application in /home/your_username/.local/bin
and installs the hello-world.desktop file in /home/your_username/.local/share/applications

You can now run the application by typing "Hello World" in the Overview.

----------------
To uninstall, type:

make uninstall

----------------
To create a tarball type:

make distcheck

This will create hello-world-1.0.tar.xz
</code>
    </section>

    <!-- TODO: How to make a custom icon with autotools -->

  </section>
</page>
