Lets Make A Gtk# App.

Matt Perley
3 min readSep 25, 2020

--

Simple example applet.

Earlier today I posted an article about how to create a new Gtk# application with .Net Core. In the image above you can see the applet we are going to put together. All it contains is a Label, an Entry and a Button. We are going to see how to put some UI together and add some simple functionality to widgets. Lets get started!

So to start out add the following code. I use images so you can’t copy and paste and are therefore forced to type it out by hand and build that memory. Sorry not sorry :)

AddGUI()

As you can see the code is far from scary. I swear! Here we can see a Vbox being added. This class allows us to pack child widgets inside in a couple of different ways. It provides: Add(widget), PackStart(widget, expand?, fill?, margin) and PackEnd(widget, expand?, fill?, margin). You can see that packstart and packend are the basically the same thing, but one anchors the widget to the top of the view and the other to the bottom. Add() creates a widget in between. This makes it super easy to Align things like menus and tab bars or other kinds of widgets in many ways.

Next we create a Label to display a title. In this case its simply “Hello World!”. We can now use the packstart function provided by Vbox to add that label to the view. Now lets add in an entry. Once done we add it to the Vbox. Finally add a Button, by using button1.Clicked += … we are able to create some logic for that button to run when we click it. We will just simply output the entry text to the console. Now we need to add the AddGUI() function to the constructor.

Add the new function to constructor.

Now that we have added everything give hit F5(or whatever your editors run button is) and run it. When you enter some text and click the button you will see it appear in the debug console. Simple right? Well this was just a basic example.

I hope this demonstrates that wandering away from the core Microsoft frameworks and API’s really is not that bad. .Net in my opinion has become a very capable player in the cross platform world. You can still get all of the syntactical pleasure of C# and the functionality of .Net but the flexibility to use it with a growing number of technologies.

Until next time, take care!

--

--