Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package require Tk
- grid [tk::canvas .canvas -width 500 -height 500 -scrollregion "0 0 1000 1000"] -sticky nwes -column 0 -row 0
- grid columnconfigure . 0 -weight 1
- grid rowconfigure . 0 -weight 1
- bind .canvas <1> "set lastx %x; set lasty %y"
- bind .canvas <B1-Motion> "addLine %x %y"
- bind .canvas <B1-ButtonRelease> "doneStroke"
- set id [.canvas create rectangle 10 10 30 30 -fill red -tags "palette palettered"]
- .canvas bind $id <1> "setColor red"
- set id [.canvas create rectangle 10 35 30 55 -fill blue -tags "palette paletteblue"]
- .canvas bind $id <1> "setColor blue"
- set id [.canvas create rectangle 10 60 30 80 -fill black -tags "palette paletteblack paletteSelected"]
- .canvas bind $id <1> "setColor black"
- proc setColor {color} {
- set ::color $color
- .canvas dtag all paletteSelected
- .canvas itemconfigure palette -outline white
- .canvas addtag paletteSelected withtag palette$color
- .canvas itemconfigure paletteSelected -outline #999999
- }
- setColor black
- .canvas itemconfigure palette -width 5
- proc addLine {x y} {
- if {![info exists ::lastx]} {
- return
- }
- if {![info exists ::lasty]} {
- return
- }
- .canvas create line $::lastx $::lasty $x $y -fill $::color -width 5 -tags currentline
- set ::lastx $x
- set ::lasty $y
- }
- proc doneStroke {} {
- .canvas itemconfigure currentline -width 1
- .canvas dtag all currentline
- }
Advertisement
Add Comment
Please, Sign In to add comment