Question

How can I change the default font and its size from a menu in a GUI I'm creating using Tcltk? The example below has only one menu called 'File', but in my GUI, It will have more than that. So all of them have to be re-sized. Is there any way to do that for my entire GUI?

Thank you in advance!

require(tcltk)
readCsv <- function(){
  myval <- tkgetOpenFile()
  mydata <- read.csv(paste(as.character(myval), collapse = " "))
  assign("myData", mydata, envir = .GlobalEnv)
}

tt <- tktoplevel()
topMenu <- tkmenu(tt)           
tkconfigure(tt, menu = topMenu) 
fileMenu <- tkmenu(topMenu, tearoff = FALSE)
tkadd(fileMenu, "command", label = "Quit", command = function() tkdestroy(tt))
tkadd(fileMenu, "command", label = "Load", command = function() readCsv())
tkadd(topMenu, "cascade", label = "File", menu = fileMenu)
tkfocus(tt)
Was it helpful?

Solution

The default font for menus is a named font; TkMenuFont on most platforms (and menu on OSX, where you really shouldn't change it). This is usually mapped to the correct system default font for menus. However, if you do want to change it, you're still recommended to use a named font (which is what is used in a font object in R TclTk) following the pattern on this page except that you're applying the font to a menu widget instead of a label.

# Example to show how to do it
fontMenu <- tkfont.create(family="times",size=24,weight="bold",slant="italic")
fileMenu <- tkmenu(topMenu, tearoff = FALSE, font = fontMenu)

The only platform where you shouldn't do this at all is OSX, where menus work rather differently (except at the script level; there's a lot of differences being hidden under the covers!)

OTHER TIPS

Yes, using the option database.

See this question for an example showing the option database for use with a button, but works the same for menus: https://stackoverflow.com/questions/20960107/is-there-a-way-to-have-a-global-style-for-button-in-tcl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top