|
| 1 | +#!/usr/bin/env wish |
| 2 | + |
| 3 | +#============================================================================== |
| 4 | +# Demonstrates the use of the Scrollutil package in connection with the BWidget |
| 5 | +# ScrollableFrame widget. |
| 6 | +# |
| 7 | +# Copyright (c) 2019 Csaba Nemethi (E-mail: csaba.nemethi@t-online.de) |
| 8 | +#============================================================================== |
| 9 | + |
| 10 | +package require Tk 8.5 |
| 11 | +package require BWidget |
| 12 | +Widget::theme yes |
| 13 | +package require scrollutil_tile |
| 14 | + |
| 15 | +wm title . "European Capitals Quiz" |
| 16 | + |
| 17 | +# |
| 18 | +# Create a ScrollableFrame within a scrollarea |
| 19 | +# |
| 20 | +set f [ttk::frame .f] |
| 21 | +set sa [scrollutil::scrollarea $f.sa] |
| 22 | +set sf [ScrollableFrame $sa.sf] |
| 23 | +$sa setwidget $sf |
| 24 | + |
| 25 | +# |
| 26 | +# Work around a tile bug which is not handled in |
| 27 | +# the BWidget procedure ScrollableFrame::create |
| 28 | +# |
| 29 | +if {$ttk::currentTheme eq "aqua"} { |
| 30 | + $sf:cmd configure -background #ececec |
| 31 | +} |
| 32 | + |
| 33 | +# |
| 34 | +# Create mouse wheel event bindings for the binding tag "all" and |
| 35 | +# register the ScrollableFrame for scrolling by these bindings |
| 36 | +# |
| 37 | +scrollutil::createWheelEventBindings all |
| 38 | +scrollutil::enableScrollingByWheel $sf |
| 39 | + |
| 40 | +# |
| 41 | +# Get the content frame and populate it |
| 42 | +# |
| 43 | + |
| 44 | +set cf [$sf getframe] |
| 45 | + |
| 46 | +set countryList { |
| 47 | + Albania Andorra Austria Belarus Belgium "Bosnia and Herzegovina" Bulgaria |
| 48 | + Croatia Cyprus "Czech Republic" Denmark Estonia Finland France Germany |
| 49 | + Greece Hungary Iceland Ireland Italy Kosovo Latvia Liechtenstein Lithuania |
| 50 | + Luxembourg Macedonia Malta Moldova Monaco Montenegro Netherlands Norway |
| 51 | + Poland Portugal Romania Russia "San Marino" Serbia Slovakia Slovenia |
| 52 | + Spain Sweden Switzerland Ukraine "United Kingdom" "Vatican City" |
| 53 | +} |
| 54 | +set capitalList { |
| 55 | + Tirana "Andorra la Vella" Vienna Minsk Brussels Sarajevo Sofia |
| 56 | + Zagreb Nicosia Prague Copenhagen Tallinn Helsinki Paris Berlin |
| 57 | + Athens Budapest Reykjavik Dublin Rome Pristina Riga Vaduz Vilnius |
| 58 | + Luxembourg Skopje Valletta Chisinau Monaco Podgorica Amsterdam Oslo |
| 59 | + Warsaw Lisbon Bucharest Moscow "San Marino" Belgrade Bratislava Ljubljana |
| 60 | + Madrid Stockholm Bern Kiev London "Vatican City" |
| 61 | +} |
| 62 | + |
| 63 | +foreach country $countryList capital $capitalList { |
| 64 | + set capitalArr($country) $capital |
| 65 | +} |
| 66 | + |
| 67 | +set capitalList [lsort $capitalList] |
| 68 | + |
| 69 | +ttk::style map TCombobox -fieldbackground {readonly white} |
| 70 | +set btnStyle [expr {$::ttk::currentTheme eq "aqua" ? "TButton" : "Toolbutton"}] |
| 71 | + |
| 72 | +set row 0 |
| 73 | +foreach country $countryList { |
| 74 | + set w [ttk::label $cf.l$row -text $country] |
| 75 | + grid $w -row $row -column 0 -sticky w -padx {5 0} -pady {4 0} |
| 76 | + |
| 77 | + set w [ttk::combobox $cf.cb$row -state readonly -width 14 \ |
| 78 | + -values $capitalList] |
| 79 | + bind $w <<ComboboxSelected>> [list checkCapital %W $country] |
| 80 | + grid $w -row $row -column 1 -sticky w -padx {5 0} -pady {4 0} |
| 81 | + |
| 82 | + # |
| 83 | + # Adapt the handling of the mouse wheel events for the ttk::combobox widget |
| 84 | + # |
| 85 | + scrollutil::adaptWheelEventHandling $w |
| 86 | + |
| 87 | + set b [ttk::button $cf.b$row -style $btnStyle -text "Resolve" \ |
| 88 | + -command [list setCapital $w $country]] |
| 89 | + grid $b -row $row -column 2 -sticky w -padx 5 -pady {4 0} |
| 90 | + |
| 91 | + incr row |
| 92 | +} |
| 93 | + |
| 94 | +# |
| 95 | +# Set the ScrollableFrame's width, height, and yscrollincrement |
| 96 | +# |
| 97 | +update idletasks |
| 98 | +set rowHeight [expr {[winfo reqheight $cf] / $row}] |
| 99 | +$sf configure -width [winfo reqwidth $cf] \ |
| 100 | + -height [expr {10*$rowHeight + 5}] -yscrollincrement $rowHeight |
| 101 | + |
| 102 | +# |
| 103 | +# Create a ttk::button widget outside the scrollarea |
| 104 | +# |
| 105 | +set b [ttk::button $f.b -text "Close" -command exit] |
| 106 | + |
| 107 | +pack $b -side bottom -pady {0 10} |
| 108 | +pack $sa -side top -expand yes -fill both -padx 10 -pady 10 |
| 109 | +pack $f -expand yes -fill both |
| 110 | + |
| 111 | +#------------------------------------------------------------------------------ |
| 112 | + |
| 113 | +proc checkCapital {w country} { |
| 114 | + $w selection clear |
| 115 | + global capitalArr |
| 116 | + if {[$w get] eq $capitalArr($country)} { |
| 117 | + $w configure -foreground "" |
| 118 | + } else { |
| 119 | + bell |
| 120 | + $w configure -foreground red |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#------------------------------------------------------------------------------ |
| 125 | + |
| 126 | +proc setCapital {w country} { |
| 127 | + $w selection clear |
| 128 | + $w configure -foreground "" |
| 129 | + global capitalArr |
| 130 | + $w set $capitalArr($country) |
| 131 | +} |
0 commit comments