【问题标题】:Adding a GIF image to a TclTk window将 GIF 图像添加到 TclTk 窗口
【发布时间】:2012-11-01 16:01:59
【问题描述】:

我希望在我的 tcltk 窗口中插入一个“正在加载”的 GIF 图像,但我无法理解它。以下是一个可重现的示例:-

backg <- 'white'
pdlg <- tktoplevel(background=backg)
tcl('wm', 'geometry', pdlg, '500x100+450+350')
tilg <- 'Package installation in progress'
tkwm.title(pdlg, tilg)
fn <- tkfont.create(family = 'helvetica', size = 12)

nwlabel <- "    The requisite packages are being installed. This may take several    \nminutes... \n"
tllab <- tklabel(pdlg, text = nwlabel, font = fn, pady = 0, background=backg)

clickEv <- tclVar(0)

OK.but <- tkbutton(pdlg, text=' Stop ', command=function() tclvalue(clickEv) <- 1, font=fn, background='grey', pady=0)

tkgrid(tllab, columnspan=1) 
tkgrid(OK.but, row=3)

tkbind(pdlg, "<Destroy>", function() tclvalue(done) <- 2)
tkfocus(pdlg)

#This allows me to insert a GIF image but the animation is lost. Also it would be convenient if the output can be obtained using 'tkgrid' instead of 'tkpack'
pdlg2 <- tktoplevel(background='white')
loading <- tclVar()
tcl('image', 'create', 'photo', loading,
file='file path to GIF file')
trial <- tklabel(pdlg2, image=loading)
tkpack(trial)

可以从这里下载一个示例 GIF 文件 -http://www.dlfpramericalife.com/library/images/final_loading_big.gif

理想情况下,GIF 图像应放置在“停止”按钮上方但文本下方。非常感谢您的帮助!

【问题讨论】:

    标签: r tcl tk


    【解决方案1】:

    在 Tcl/Tk 中,这很容易。

    set img [image create photo -file nameofthefile.gif]
    label .l -image $img
    

    我不知道 R,但以你的代码为指导,我想像这样,但请检查一下!

    img <- tkimage.create('photo', file='nameofthefile.gif')
    imglab <- tklabel(pdlg, image = img)
    

    ...然后您将其网格/打包/放置在您想要的任何地方。请注意,这不适用于动画 gif,我认为动画必须是手动处理程序,使用定期更新图像内容的计时器,但我从未这样做过,也不知道该怎么做。您可以查看Tcl/Tk wiki 以获得更多帮助。

    【讨论】:

    • 我们从未做过动画 GIF 的实际动画部分(尽管您仍然可以选择显示动画的哪一帧)。有些人可能会认为这是一项功能!
    猜你喜欢
    • 2019-05-24
    • 2019-09-16
    • 2019-11-16
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    相关资源
    最近更新 更多