ToggleButton은 보통 Button이지만 눌렀을 때 눌린 상태라는 점은 다릅니다. 이 예제에서 Spinner와 같은 매개를 다룰때 켬/끔 스위치로 사용할 수 있습니다.
ToggleButton의 get_active()
메서드는 눌렸을 때 true를 반환합니다. 그리고 눌린 상태가 아니면 false를 반환합니다. set_active()
메서드는 단추를 누르지 않아도 상태를 설정하고 싶을 때 사용합니다. 눌려있거나 튀어나왔거나 하는 상태를 바꿀 때, 어떤 함수와 연결할 수 있는 "toggled" 시그널을 내보냅니다.
#!/usr/bin/gjs
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
이 프로그램을 실행할 때 가져올 라이브러리입니다. 시작 부분에 항상 gjs가 필요함을 알리는 줄을 작성해야 함을 기억하십시오.
const ToggleButtonExample = new Lang.Class({
Name: 'ToggleButton Example',
// Create the application itself
_init: function() {
this.application = new Gtk.Application({
application_id: 'org.example.jstogglebutton',
flags: Gio.ApplicationFlags.FLAGS_NONE
});
// Connect 'activate' and 'startup' signals to the callback functions
this.application.connect('activate', Lang.bind(this, this._onActivate));
this.application.connect('startup', Lang.bind(this, this._onStartup));
},
// Callback function for 'activate' signal presents window when active
_onActivate: function() {
this._window.present();
},
// Callback function for 'startup' signal builds the UI
_onStartup: function() {
this._buildUI ();
},
이 예제의 모든 코드는 RadioButtonExample 클래스에 들어갑니다. 위 코드는 위젯과 창이 들어가는 Gtk.Application을 만듭니다.
// Build the application's UI
_buildUI: function() {
// Create the application window
this._window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
default_height: 300,
default_width: 300,
border_width: 30,
title: "ToggleButton Example"});
_buildUI 함수는 프로그램 사용자 인터페이스를 만드는 모든 코드를 넣는 곳입니다. 첫 단계에서는 모든 위젯을 우겨넣을 새 Gtk.ApplicationWindow를 만듭니다.
_onToggle: function() {
// Start or stop the spinner
if (this._toggleButton.get_active ())
this._spinner.start ();
else this._spinner.stop ();
}
});
단추 눌린 상태를 누군가가 바꾸면, 이 함수에서 get_active() 함수를 사용하여 상태를 확인한 다음에 바로 스피너 동작을 시작하거나 멈춥니다. 우리는 단추가 눌린 동안에만 스피너가 돌아가게 하려고 하기에 get_active에서 true를 반환할 경우에 스피너 회전을 시작하겠습니다. 아니면 멈추라고 하고요.
// Run the application
let app = new ToggleButtonExample ();
app.application.run (ARGV);
마지막으로, 마무리 지은 RadioButtonExample 클래스의 새 인스턴스를 만들고 프로그램 실행을 설정하겠습니다.
#!/usr/bin/gjs
imports.gi.versions.Gtk = '3.0';
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
class ToggleButtonExample {
// Create the application itself
constructor() {
this.application = new Gtk.Application({
application_id: 'org.example.jstogglebutton',
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: 300,
default_width: 300,
border_width: 30,
title: "ToggleButton Example"});
// Create the spinner that the button stops and starts
this._spinner = new Gtk.Spinner ({hexpand: true, vexpand: true});
// Create the togglebutton that starts and stops the spinner
this._toggleButton = new Gtk.ToggleButton ({label: "Start/Stop"});
this._toggleButton.connect ('toggled', this._onToggle.bind(this));
// Create a grid and put everything in it
this._grid = new Gtk.Grid ({
row_homogeneous: false,
row_spacing: 15});
this._grid.attach (this._spinner, 0, 0, 1, 1);
this._grid.attach (this._toggleButton, 0, 1, 1, 1);
// Add the grid to the window
this._window.add (this._grid);
// Show the window and all child widgets
this._window.show_all();
}
_onToggle() {
// Start or stop the spinner
if (this._toggleButton.get_active ())
this._spinner.start ();
else this._spinner.stop ();
}
};
// Run the application
let app = new ToggleButtonExample ();
app.application.run (ARGV);
Gtk.Application
Gtk.ApplicationWindow
Gtk.Grid
Gtk.Spinner
Gtk.ToggleButton