이 프로그램에는 CheckButton이 있습니다. 이 확인 상자의 표시 여부에 따라 창 제목 표시줄에 무언가를 나타냅니다.
CheckButton에 표시하거나 지울 때, "toggled" 시그널을 보냅니다. 표시해두면 "active" 속성 값이 true가 되고, 그렇지 않으면 false가 됩니다.
이 프로그램을 실행할 때 가져올 라이브러리입니다. 시작 부분에 항상 gjs가 필요함을 알리는 줄을 작성해야 함을 기억하십시오.
이 예제의 모든 코드는 CheckButtonExample 클래스에 들어갑니다. 위 코드에서는 위젯과 창이 들어가는 Gtk.Application을 만듭니다.
_buildUI 함수는 프로그램 사용자 인터페이스를 구성하는 모든 코드를 넣는 곳입니다. 우선 모든 위젯을 넣을 새 Gtk.ApplicationWindow 를 만들겠습니다.
CheckButton 표시를 지운 상태로 바꿀 때 창 제목을 사라지게 하려고 합니다. 다시 이걸 표시해두면 창 제목을 다시 나타나게 하려고 합니다. 단추가 활성(표시) 상태인지 여부를 나중에 확인하는 방식으로 처리할 수 있습니다. 이 과정에서 CheckButton의 get_active() 메서드를 호출하는 간단한 if/else 구문을 사용하겠습니다.
// Run the application
let app = new CheckButtonExample ();
app.application.run (ARGV);
마지막으로 완성본 CheckButtonExample 클래스의 새 인스턴스를 만들고 프로그램 실행을 설정하겠습니다.
#!/usr/bin/gjs
imports.gi.versions.Gtk = '3.0';
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
class CheckButtonExample {
// Create the application itself
constructor() {
this.application = new Gtk.Application({
application_id: 'org.example.jscheckbutton',
flags: Gio.ApplicationFlags.FLAGS_NONE
});
// Connect 'activate' and 'startup' signals to the callback functions
this.application.connect('activate', this._onActivate.bind(this));
this.application.connect('startup', this._onStartup.bind(this));
}
// Callback function for 'activate' signal presents window when active
_onActivate() {
this._window.present();
}
// Callback function for 'startup' signal builds the UI
_onStartup() {
this._buildUI();
}
// Build the application's UI
_buildUI() {
// Create the application window
this._window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
default_height: 100,
default_width: 300,
border_width: 10,
title: "CheckButton Example"});
// Create the check button
this._button = new Gtk.CheckButton ({label: "Show Title"});
this._window.add (this._button);
// Have the check button be checked by default
this._button.set_active (true);
// Connect the button to a function that does something when it's toggled
this._button.connect ("toggled", this._toggledCB.bind(this));
// Show the window and all child widgets
this._window.show_all();
}
_toggledCB() {
// Make the window title appear or disappear when the checkbox is toggled
if (this._button.get_active() == true)
this._window.set_title ("CheckButton Example");
else
this._window.set_title ("");
}
};
// Run the application
let app = new CheckButtonExample ();
app.application.run (ARGV);
Gtk.Application
Gtk.ApplicationWindow
Gtk.CheckButton