Last active
June 3, 2024 11:11
-
-
Save giuliohome/d9aa661b16b30a7e94245a65305266d0 to your computer and use it in GitHub Desktop.
#fsharp 🤝 #gtk 4 T
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: GirCore.Gtk-4.0" | |
open Gtk | |
// https://mastodon.social/@lamg__/112546558109306665 | |
let label () = | |
let lb = new Label() | |
lb.SetText "Hello!" | |
lb | |
let button ( label: Label ) = | |
let btn = new Button () | |
btn.SetLabel "click me!" | |
let mutable counter = 0 | |
let clickHler (_) (_) = | |
label.SetText $"Hello {counter}" | |
counter <- counter + 1 | |
btn.add_OnClicked(new GObject.SignalHandler<Button>(clickHler)) | |
btn | |
let onActivate (sender: Gio.Application) (_) = | |
let win = ApplicationWindow.New(sender :?> Application) | |
win.Title <- "GTK4 Window" | |
win.SetDefaultSize(300, 300) | |
let box = new Box() | |
let lb = label() | |
box.Append lb | |
win.SetChild( box ) | |
button lb |> box.Append | |
win.Show() | |
let applic = Application.New("org.gir.core", Gio.ApplicationFlags.FlagsNone) | |
applic.add_OnActivate(new GObject.SignalHandler<Gio.Application>(onActivate)) | |
applic.RunWithSynchronizationContext(null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk | |
class ClickCounterWindow(Gtk.Window): | |
def __init__(self): | |
super().__init__(title="Click Counter") | |
self.set_border_width(10) | |
# Set the default window size | |
self.set_default_size(300, 300) | |
# Initialize the click count | |
self.click_count = 0 | |
# Create a button with an initial label | |
self.button = Gtk.Button(label=f"Clicks: {self.click_count}") | |
self.button.connect("clicked", self.on_button_clicked) | |
# Set alignment of the button inside the box | |
self.button.set_halign(Gtk.Align.CENTER) | |
self.button.set_valign(Gtk.Align.CENTER) | |
# Create a Box to contain the button | |
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) | |
self.box.pack_start(self.button, expand=False, fill=False, padding=0) | |
# Set the size request for the box | |
self.box.set_size_request(100, 100) | |
# Set alignment of the box | |
self.box.set_halign(Gtk.Align.CENTER) | |
self.box.set_valign(Gtk.Align.CENTER) | |
# Add the box to the window | |
self.add(self.box) | |
def on_button_clicked(self, widget): | |
# Increment the click count | |
self.click_count += 1 | |
# Update the button label | |
self.button.set_label(f"Clicks: {self.click_count}") | |
# Create and run the GTK application | |
win = ClickCounterWindow() | |
win.connect("destroy", Gtk.main_quit) | |
win.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment