如何在Ruby TK中创建日历小部件

问题描述 投票:0回答:1

我正在尝试使用以下代码在Ruby Tk中创建日历小部件:

require 'tk'
require 'tkextlib/iwidgets'

DEBUG = []

begin
    root = TkRoot.new {title 'Ruby/Tk Calendar'}

    cal = Tk::Iwidgets::Calendar.new(root) {
       outline 'black'
       weekdaybackground 'gray90'
       weekendbackground 'white'
       command {p cal.get}
    }
    cal.pack('pady'=>10)
    cal.show('11/1/2006')

    # Set initial window geometry; i.e., size and placement.
    win_w, win_h = 250, 195
    # root.minsize(win_w, win_h)
    win_lf = (root.winfo_screenwidth - win_w) / 2
    root.geometry("#{win_w}x#{win_h}+#{win_lf}+50")

    # Set resize permissions.
    root.resizable(false, false)

    # Make Cmnd+Q work as expected.
    root.bind('Command-q') {Tk.root.destroy}

    Tk.mainloop
ensure
    puts DEBUG unless DEBUG.empty?
end

但是,我收到以下消息:

找不到包Itk(RuntimeError)

TkPackage找不到包Itk(RuntimeError)

我已经安装了tcl软件包,但仍然无法正常工作。我正在将Fedora 31与ruby 2.6.5一起使用。

ruby calendar widget tk
1个回答
1
投票

此答案将安装Ruby的最新版本(在撰写本文时),以及Tcl的最新兼容版本。

注:安装Ruby 2.7.0后,$ gem install tk说“不支持Tcl / Tk8.6 [;],它将无法正常工作。”因此,我们必须将Tcl的使用限制为8.5版。我们将通过安装ActiveTcl 8.5版来做到这一点。

这些步骤适用于Debian Stretch,因此适用于YMMV的Fedora 31。 :)

创建一些目录:

$ mkdir ~/install
$ mkdir ~/install/temp
$ mkdir ~/progra

使用网络浏览器,从ActiveState下载ActiveTcl 8.5。然后,安装它:

$ pushd ~/install/temp
$ tar zxf ~/Downloads/ActiveTcl-8.5*.tar.gz
$ cd ActiveTcl-8.5*
$ ./install.sh

回答其安装问题:

Please specify the installation directory.
Path [/opt/ActiveTcl-8.5]: ~/progra/ActiveTcl-8.5

Please specify the directory for the demos.
Path [~/progra/ActiveTcl-8.5/demos]: 

Please specify the runtime installation directory.
Path [~/progra/ActiveTcl-8.5]:
$ echo 'export PATH="$HOME/progra/ActiveTcl-8.5/bin:$PATH"' >> ~/.bashrc
$ echo 'export MANPATH="$HOME/progra/ActiveTcl-8.5/bin/man:$MANPATH"' >> ~/.bashrc

安装rvm所需的一些系统软件包:

$ sudo apt-get install curl dirmngr gnupg

根据rvm(Ruby enVironment Manager)的instructions,按照以下步骤安装:

$ \curl -sSL https://get.rvm.io | bash -s -- --ignore-dotfiles
$ echo '# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.' >> ~/.bashrc
$ echo 'export PATH="$PATH:$HOME/.rvm/bin"' >> ~/.bashrc
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*' >> ~/.bashrc
$ exit

检查并使用rvm

$ type rvm | head -n 1 # It should say, 'rvm is a function'.
$ rvm list known
$ rvm install 2.7.0 --enable-shared --enable-pthread --with-tk --with-tcl

安装Tk gem所需的系统软件包:

$ sudo apt-get install libx11-dev

安装Tk gem并检查Tk的安装:

$ gem install tk
$ ruby -W0 -e "require 'tk'; p Tk::TK_PATCHLEVEL"
$ ruby -W0 -e "require 'tk'; require 'tkextlib/iwidgets'; p 'ok'"

现在,当我运行您的程序时,我看到一个日历小部件。

© www.soinside.com 2019 - 2024. All rights reserved.