#!/usr/bin/env wish # ============================================= # Pythonista Calculator → Tcl/Tk (Final polished) # Exact button heights, = button now perfectly flush # ============================================= package require Tk wm title . "Calculator" wm geometry . "320x410" ;# adjusted for perfect bottom fit wm resizable . 0 0 set shows_result 0 set top_bg "#dafffd" set btn_bg "#ffffff" set btn_bd_color "#c0c0c0" set op_fg "#007aff" # ------------------------------------------------- # Logic # ------------------------------------------------- proc button_tapped {t} { global shows_result set label1 .label1 set label2 .label2 set current_text [$label1 cget -text] if {[string is digit $t]} { if {$shows_result || $current_text eq "0"} { $label1 configure -text $t } else { $label1 configure -text $current_text$t } } elseif {$t eq "." && [string index $current_text end] ne "."} { $label1 configure -text $current_text$t } elseif {$t in {+ - ÷ ×}} { set last_char [string index $current_text end] if {[string first $last_char "+-÷×"] != -1} { $label1 configure -text [string range $current_text 0 end-1]$t } else { $label1 configure -text $current_text$t } } elseif {$t eq "AC"} { $label1 configure -text "0" } elseif {$t eq "C"} { set new_text [string range $current_text 0 end-1] if {$new_text eq ""} { set new_text "0" } $label1 configure -text $new_text } elseif {$t eq "="} { set expr_str [string map {÷ / × *} [$label1 cget -text]] if {[catch {set result [expr $expr_str]}]} { set result "ERROR" } $label2 configure -text "[$label1 cget -text] =" $label1 configure -text $result set shows_result 1 return } if {$t ne "="} { set shows_result 0 $label2 configure -text "" } } proc copy_action {} { set t1 [.label1 cget -text] set t2 [.label2 cget -text] set text [expr {$t2 ne "" ? "$t2 $t1" : $t1}] clipboard clear clipboard append $text } # ------------------------------------------------- # Tall button creator # ------------------------------------------------- proc make_button {name text x y w h fg cmd} { global btn_bg btn_bd_color frame $name -background $btn_bg -bd 2 -relief raised -highlightthickness 0 \ -highlightbackground $btn_bd_color label $name.lbl -text $text -font {Helvetica 24 bold} \ -background $btn_bg -foreground $fg -anchor center pack $name.lbl -fill both -expand 1 bind $name $cmd bind $name.lbl $cmd place $name -x $x -y $y -width $w -height $h } # ------------------------------------------------- # UI # ------------------------------------------------- frame .top -background $top_bg -width 320 -height 82 place .top -x 0 -y 0 label .label2 -text "" -font {Helvetica 14} -anchor e -justify right \ -foreground "#7f7f7f" -background $top_bg place .label2 -x 6 -y 6 -width 304 -height 26 label .label1 -text "0" -font {Helvetica 30} -anchor e -justify right \ -foreground black -background $top_bg place .label1 -x 6 -y 31 -width 304 -height 51 button .copy -text "📋" -font {Helvetica 15} -background white -foreground black \ -relief raised -bd 1 -command copy_action place .copy -x 6 -y 40 -width 40 -height 42 # Button rows (54 px tall) make_button .ac "AC" 6 90 70 54 red {button_tapped "AC"} make_button .c "C" 84 90 70 54 red {button_tapped "C"} make_button .div "÷" 162 90 70 54 $op_fg {button_tapped "÷"} make_button .mul "×" 240 90 70 54 $op_fg {button_tapped "×"} make_button .b7 "7" 6 152 70 54 black {button_tapped "7"} make_button .b8 "8" 84 152 70 54 black {button_tapped "8"} make_button .b9 "9" 162 152 70 54 black {button_tapped "9"} make_button .sub "-" 240 152 70 54 $op_fg {button_tapped "-"} make_button .b4 "4" 6 214 70 54 black {button_tapped "4"} make_button .b5 "5" 84 214 70 54 black {button_tapped "5"} make_button .b6 "6" 162 214 70 54 black {button_tapped "6"} make_button .add "+" 240 214 70 54 $op_fg {button_tapped "+"} make_button .b1 "1" 6 276 70 54 black {button_tapped "1"} make_button .b2 "2" 84 276 70 54 black {button_tapped "2"} make_button .b3 "3" 162 276 70 54 black {button_tapped "3"} # Equals button: now perfectly spans from row 4 down to bottom row (flush) frame .eq -background $btn_bg -bd 2 -relief raised -highlightthickness 0 label .eq.lbl -text "=" -font {Helvetica 24 bold} -background $btn_bg -foreground $op_fg -anchor center pack .eq.lbl -fill both -expand 1 bind .eq {button_tapped "="} bind .eq.lbl {button_tapped "="} place .eq -x 240 -y 276 -width 70 -height 116 ;# increased to close the gap # Bottom row (0 and .) make_button .b0 "0" 6 338 148 54 black {button_tapped "0"} make_button .dot "." 162 338 70 54 black {button_tapped "."}