Програми для обліку

Українське програмне забезпечення з відкритим кодом

Gtk4

Дописи

Gtk / Gtk4

17.07.2025 16:12 Gtk4

Розробка програм на C# з графічним інтерфейсом Gtk4 | Grid - Таблиця

Приклад використання віджету Grid - Таблиця

Gtk4 Grid

Для програмування використовую Visual Studio Code.
В програмі Visual Studio Code створюємо новий проект
dotnet new console

Додаємо до проекту пакет GirCore.Gtk
dotnet add package GirCore.Gtk-4.0

Запуск програми
dotnet run

При старті програми додатково завантажується файл Default.css - css стилі. Цей файл при компілюванні програми копіюється в папку збірки - це вказано в проекті <Content Include="Default.css" CopyToOutputDirectory="PreserveNewest" />

Program.cs
using Gtk;
using Gdk;

class Program
{
    static void Main()
    {
        var app = Application.New("ua.org.accounting.window", Gio.ApplicationFlags.FlagsNone);

        app.OnActivate += (_, args) =>
        {
            GridWindow firstWindow = new(app);
            firstWindow.Show();
        };

        //Css
        {
            string styleDefaultFile = Path.Combine(AppContext.BaseDirectory, "Default.css");
            var displayDefault = Display.GetDefault();

            if (File.Exists(styleDefaultFile) && displayDefault != null)
            {
                CssProvider provider = CssProvider.New();
                provider.LoadFromPath(styleDefaultFile);
                StyleContext.AddProviderForDisplay(displayDefault, provider, 800);
            }
        }

        app.RunWithSynchronizationContext(null);
    }
}

GridWindow.cs
using Gtk;
using static Gtk.Orientation;

class GridWindow : Window
{
    enum Column
    {
        Caption,
        Field,
        Help
    }

    ComboBoxText comboBoxUser;
    PasswordEntry password;

    public GridWindow(Application? app) : base()
    {
        Application = app;
        Title = "Авторизація";
        Resizable = false;
        Modal = true;

        Box vBox = Box.New(Vertical, 0);
        vBox.MarginTop = vBox.MarginBottom = vBox.MarginStart = vBox.MarginEnd = 10;
        Child = vBox;

        Grid grid = Grid.New();
        grid.ColumnSpacing = grid.RowSpacing = 10;
        vBox.Append(grid);

        int row = 0;

        //Користувач
        {
            Label labelCaption = Label.New("Користувач:");
            labelCaption.Halign = Align.End;
            grid.Attach(labelCaption, (int)Column.Caption, row, 1, 1);

            comboBoxUser = new ComboBoxText();
            comboBoxUser.Append("user1", "User 1");
            comboBoxUser.Append("user2", "User 2");

            grid.Attach(comboBoxUser, (int)Column.Field, row, 1, 1);

            Label labelHelp = Label.New("Виберіть користувача");
            labelHelp.Halign = Align.Start;

            grid.Attach(labelHelp, (int)Column.Help, row, 1, 1);
        }

        //Пароль
        {
            row++;

            Label labelCaption = Label.New("Пароль:");
            labelCaption.Halign = Align.End;
            grid.Attach(labelCaption, (int)Column.Caption, row, 1, 1);

            password = new PasswordEntry() { WidthRequest = 300, ShowPeekIcon = true };
            grid.Attach(password, (int)Column.Field, row, 1, 1);

            Label labelHelp = Label.New("Введіть пароль");
            labelHelp.Halign = Align.Start;

            grid.Attach(labelHelp, (int)Column.Help, row, 1, 1);
        }

        Separator separator = Separator.New(Vertical);
        separator.MarginTop = separator.MarginBottom = 10;
        vBox.Append(separator);

        //Кнопки
        {
            Box hBox = Box.New(Horizontal, 0);
            hBox.Halign = Align.Center;
            vBox.Append(hBox);

            {
                Button button = Button.NewWithLabel("Авторизація");
                button.MarginStart = button.MarginEnd = 3;
                button.AddCssClass("ok");
                button.OnClicked += (_, _) =>
                {
                    string? user = comboBoxUser.ActiveId;
                    string? pass = password.Text_;

                    if (!string.IsNullOrEmpty(user) && pass != null)
                    {
                        // Перевірка пароля
                    }
                    else
                        Message.Error(Application, this, "Помилка", "Не заповнені поля!");
                };

                hBox.Append(button);
            }

            {
                Button button = Button.NewWithLabel("Відмінити");
                button.MarginStart = button.MarginEnd = 3;
                button.AddCssClass("ok");
                button.OnClicked += (_, _) => Close();
                hBox.Append(button);
            }
        }
    }

    class Message()
    {
        public static void Error(Application? app, Window? win, string text, string? secondaryText = null)
        {
            MessageDialog message = new()
            {
                TransientFor = win,
                Application = app,
                Modal = true,
                Valign = Align.Center,
                Halign = Align.Center,
                Text = text,
                SecondaryText = secondaryText
            };

            message.AddButton("Закрити", 1);

            message.OnResponse += (_, _) =>
            {
                message.Hide();
                message.Destroy();
            };

            message.Show();
        }
    }
}

Default.css
button.ok {
    padding: 12px 50px;
    border-radius: 20px;
}

Grid.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GirCore.Gtk-4.0" Version="0.7.0-preview.1" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="Default.css" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

</Project>

Довідка: Як встановити Gtk4 для Linux та Windows

Gtk4 - кросплатформовий набір інструментів для створення графічних інтерфейсів користувача
Gir.Core - обгортка над бібліотеками Gtk4 для мови програмування C#

Visual Studio Code
Віджети Gtk 4
NuGet пакет GirCore.Gtk-4.0
Приклад на GitHub


© accounting.org.ua - 2025