First Steps
This application opens a native window containing a button. Clicking the button displays a message box.
This is the regular object-oriented API used throughout most of the guide. The equivalent block-based form is described in Coding Styles.
require "uing"
UIng.init
window = UIng::Window.new("Hello World", 300, 200)
window.on_closing do
UIng.quit
true
end
button = UIng::Button.new("Click me")
button.on_clicked do
window.msg_box("UIng", "Hello from Crystal!")
end
window.child = button
window.show
UIng.main
UIng.uninit
Save the source as hello.cr, then run it:
crystal run hello.cr
How it works
UIng.initinitializes the native GUI backend.Window.newandButton.newcreate native controls.on_clickedregisters code to run after a click.- Assigning
window.childplaces the button in the window. window.showdisplays the interface.UIng.mainruns the event loop until the closing callback callsUIng.quit.UIng.uninitreleases application-wide resources after the loop ends.
Returning true from on_closing allows the window to close. UIng.init
also accepts a block; in that case UIng.uninit is called automatically
after the event loop returns. See
Runtime and Lifetime for details.