R:在Mac和Linux中更改Tcltk窗口的图标

时间:2012-10-01 05:50:12

标签: macos r icons tcl tk

我已经创建了一个进度条来密切关注某些R脚本的执行情况。我想在栏中插入一个自定义图标,而不是默认的'Tk'图标。我可以使用.ico文件和以下命令在Windows上执行此操作

tcl('wm', 'iconbitmap', .win, 'Icon.ico')

但我不知道如何在Mac OSX和Linux中做同样的事情。显然,.ico格式不起作用,但.png,.jpg,.bmp,.xbm或.xpm也不起作用。关于我如何进行的任何建议?下面附有示例图片和进度条形码: -

Sample image http://tinypic.com/r/jt8efn/6 - http://tinypic.com/r/jt8efn/6

tkProgressBar2 <- function (title = 'Test progress bar', label = '', min = 0, max = 100, initial = 0, width = 300, userfn='helvetica', backg='white') {
  useText <- FALSE
  have_ttk <- as.character(tcl('info', 'tclversion')) >= '8.5'
  if (!have_ttk && as.character(tclRequire('PBar')) == 'FALSE') useText <- TRUE
  .win <<- tktoplevel(background=backg)
  tkfocus()
  tcl('wm', 'geometry', .win, '500x100+450+350')
  tcl('wm', 'iconbitmap', .win, '@Icon.xbm')
  .val <- initial
  .killed <- FALSE
  tkwm.geometry(.win, sprintf('%dx80', width + 40))
  tkwm.title(.win, title)
  fn <- tkfont.create(family = userfn, size = 12)
  if (useText) {
    .lab <- tklabel(.win, text = label, font = fn, padx = 0, background=backg)
    tkpack(.lab, side = 'left')
    fn2 <- tkfont.create(family = userfn, size = 16)
    .vlab <- tklabel(.win, text = '0%', font = fn2, padx = 20, background=backg)
    tkpack(.vlab, side = 'right')
    up <- function(value) {
      if (!is.finite(value) || value < min || value > max) return()
      .val <<- value
      tkconfigure(.vlab, text = sprintf('%d%%', round(100 * (value - min)/(max - min))))
    }
  } else {
    .lab <- tklabel(.win, text = label, font = fn, pady = 0, background=backg)
    .tkval <- tclVar(0)
    tkpack(.lab, side = 'top')
    tkpack(tklabel(.win, text = '', font = fn, background=backg), side = 'bottom')
    pBar <- if (have_ttk)
      ttkprogressbar(.win, length = width, variable = .tkval) else
        tkwidget(.win, 'ProgressBar', width = width, variable = .tkval)
    tkpack(pBar, side = 'bottom')
    up <- function(value) {
      if (!is.finite(value) || value < min || value > max) return()
      .val <<- value
      tclvalue(.tkval) <<- 100 * (value - min)/(max - min)
    }
  }
  getVal <- function() .val
  kill <- function() if (!.killed) {
    tkdestroy(.win)
    .killed <<- TRUE
  }
  title <- function(title) tkwm.title(.win, title)
  lab <- function(label) tkconfigure(.lab, text = label)
  tkbind(.win, '<Destroy>', function() stop())
  up(initial)
  structure(list(getVal = getVal, up = up, title = title, label = lab, kill = kill), class = 'tkProgressBar')
}

pb <- tkProgressBar2(title='Performing k-Means clustering', label='Some information in %', min=0, max=100, initial=0, width=400, userfn='verdana', backg='white')

1 个答案:

答案 0 :(得分:0)

在Linux上,您可以使用wm iconphoto设置图标; wm iconbitmap完全做了别的事情。为此,您需要创建一个包含图像数据的photo image

我猜你在R中写这个:

tcl('wm', 'iconphoto', .win, tcl('image', 'create', 'photo', '-file', 'Icon.gif'))

我不太确定您使用的Tk版本支持哪些图像格式,包括它可用的任何图像格式支持包。最小的设置是GIF和PPM,除非您(勇敢地)使用8.6,当PNG默认也可用时。

(您也可以以编程方式创建照片图像的内容,但由于各种原因,这种情况很慢。)

OSX没有相同意义上的窗口图标;每个最小化窗口在最小化到停靠点时只显示自身的快照是正常的。