我有一个带有照片图像的GUI。窗口开始时与原始图像的大小完全相同。我想要的是图像在用户调整窗口大小时调整自身大小以适应窗口。基于“照片”命令的文档,我尝试了以下内容。但它似乎没有用。图像总是保持相同的大小,并且调整窗口大小只是隐藏它的一部分而不是调整它的大小。我不理解-shrink选项的功能吗?
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
}
答案 0 :(得分:1)
答案是:
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
}