Question

I have a GUI with a photo image in it. The window starts out being exactly the size of the native image. What I would like is for the image to resize itself to fit the window when the user resizes the window. Based on the documentation from "photo" command, I have tried the follwing. But it does not seem to work. The image just stays the same size always and resizing the window just hides part of it instead of resizing it. Am I not understanding the function of the -shrink option?

package require Tk

image create photo previewraw -file preview.pnm
image create photo preview -file preview.pnm

ttk::frame .c
ttk::label .c.preview -image preview

grid .c -column 0 -row 0 -sticky nwes
grid .c.preview -column 0 -row 0
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
grid columnconfigure .c 0 -weight 1
grid rowconfigure .c 0 -weight 1

bind .c.preview <Configure> {
    preview copy previewraw -to 0 0 %w %h -shrink
}
Was it helpful?

Solution

and the anwer is:

package require Tk

image create photo preview -file preview.pnm

ttk::frame .c
ttk::label .c.preview -image preview

grid .c -column 0 -row 0 -sticky nwes
grid .c.preview -column 0 -row 0
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
grid columnconfigure .c 0 -weight 1
grid rowconfigure .c 0 -weight 1

bind .c.preview <Configure> { 
    exec convert preview.pnm -resize %wx%h -background lightgray -gravity center -extent %wx%h newfile
    preview read newfile -shrink
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top