GTK# (GTK3 / C# / Glade / Visual Studio Code) - Linux

A Place to discuss programming languages, APIs and software development
Post Reply
User avatar
ZerOne
Site Admin
Posts: 116
Joined: Sun Dec 13, 2020 8:21 am

Introduction
GtkSharp is a .NET wrapper for the GTK library, enabling developers to create graphical user interfaces (GUIs) in C#.
It provides bindings for GTK and related libraries such as GLib, GDK, Pango, and Cairo, making it a powerful tool for cross-platform application development.

This quick guide introduces how to develop applications using GTK#
Specifically it includes instructions on how to use Glade and Visual Studio Code to write a C# application using GTK3.

This includes
* Installing the required prerequisites on Linux (Debian/Ubuntu/Mint)
* Installing Glade (The Interface Designer)
* Installing Visual Studio Code
* Creating your first GTK# Program.


Key Features of GtkSharp
GtkSharp supports GTK 3.22 and is compatible with .NET Standard 2.0, making it suitable for modern .NET applications.
Unlike older versions, it does not require glue libraries, simplifying the setup process.
User avatar
ZerOne
Site Admin
Posts: 116
Joined: Sun Dec 13, 2020 8:21 am

Installing the Prerequisites

1) Install Glade, using your linux package manager

Code: Select all

sudo apt install glade
2) Download the .NET framework for your operating system, using the following LINK

3) Install the .NET DEB Package.

4) Download Visual Studio Code for your operating system using this LINK

5) Install the Visual Studio Code .DEB package

6) Open Visual Studio Code

7) On the top menu bar in Visual Studio Code, click on View -> Extensions to bring up the extensions pane

8) Search for the extension C# Dev Kit extension, from Microsoft, and install

9) Search for the extension C# extension, from Microsoft, and install
You do not have the required permissions to view the files attached to this post.
User avatar
ZerOne
Site Admin
Posts: 116
Joined: Sun Dec 13, 2020 8:21 am

To use GtkSharp, you need to install the necessary NuGet packages.
The primary package is GtkSharp, but additional libraries like GdkSharp, PangoSharp, and CairoSharp can be included depending on your requirements.
For example, you can install the main package using the following command:

Code: Select all

dotnet add package GtkSharp
The following is a Guide on Installing GTK# on Mac OSX
User avatar
ZerOne
Site Admin
Posts: 116
Joined: Sun Dec 13, 2020 8:21 am

Creating a GTK Application in C#

You can quickly scaffold a new GTK application using the dotnet new templating engine.
First, install the GtkSharp template by running the following in a Terminal window.

Code: Select all

dotnet new --install GtkSharp.Template.CSharp
Then, generate a new GTK application project:

Code: Select all

dotnet new gtkapp -n MyGtkApp
This creates a basic GTK application structure that you can build upon.
User avatar
ZerOne
Site Admin
Posts: 116
Joined: Sun Dec 13, 2020 8:21 am

Example: A Simple GTK Window in C#

Here’s an example of creating a simple GTK window using GtkSharp:

Code: Select all

using Gtk;
class Program
{
   static void Main(string[] args)
   {
       Application.Init(); // Initialize the GTK application
       // Create a new window
       var window = new Window("Hello, GtkSharp!");
       window.SetDefaultSize(400, 300);
       window.DeleteEvent += (o, e) => Application.Quit(); // Close the app on window close
       // Add a label to the window
       var label = new Label("Welcome to GtkSharp!");
       window.Add(label);
       window.ShowAll(); // Display the window and its contents
       Application.Run(); // Run the GTK application loop
   }
}
Save this code in a .cs file, build it using dotnet build, and run the application to see a simple GTK window.
Post Reply

Return to “Programming and API Forums”