Skip to content

Instantly share code, notes, and snippets.

@kujirahand
Created July 16, 2024 12:46
Show Gist options
  • Save kujirahand/82355b30485aa92716aa5bf1c8f610df to your computer and use it in GitHub Desktop.
Save kujirahand/82355b30485aa92716aa5bf1c8f610df to your computer and use it in GitHub Desktop.
Rustでデスクトップアプリを作ろう unit_converter/src/main.rs
use tk::*;
use tk::cmd::*;
use tcl::*;
fn main() -> TkResult<()> {
// Tkの初期化
let tk = make_tk!()?;
let root = tk.root();
root.set_wm_title("インチからセンチへの変換")?;
// ウィンドウ上にフレームを作成
let c = root
.add_ttk_frame(())?
.pack(())?;
c.configure( -padding(12) )?; // 余白を設定
// Gridレイアウトを利用してウィジェットを配置 --- (*1)
c.add_ttk_label( "inch_lbl" -text("インチ") )?
.grid( -column(1) -row(1) -sticky("ne") )?;
c.add_entry( "inch_entry" -textvariable("inch_var") )?
.grid( -column(2) -row(1) -sticky("nw") )?;
c.add_button( "calc" -text("計算") -command("calculate") )?
.grid( -column(3) -row(1) -sticky("w") )?;
c.add_ttk_label( "cm_lbl" -text("センチ") )?
.grid( -column(1) -row(2) -sticky("ne") )?;
c.add_entry( "cm_entry" -textvariable("cm_var") )?
.grid( -column(2) -row(2) -sticky("nw") )?;
// Entryに初期値を設定 --- (*2)
tk.run(("set", "inch_var", "30.0"))?;
// 計算関数を定義 --- (*3)
#[proc] fn calculate() -> TkResult<()> {
let interp = tcl_interp!();
let inch: f64 = interp.get_double("inch_var")?;
let cm = inch * 2.54;
interp.set_double("cm_var", cm);
Ok(())
}
// 定義した関数を登録 --- (*4)
unsafe{ tk.def_proc( "calculate", calculate ); }
// メインループ
Ok( main_loop() )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment