如何在 Ruby TK 中创建日历小部件
How to create a Calendar widget in Ruby TK
我正在尝试使用以下代码在 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(运行时错误)
TkPackage 找不到包 Itk(运行时错误)
我已经安装了 tcl 软件包,但仍然无法正常工作。我在 ruby 2.6.5.
中使用 Fedora 31
此答案将安装最新版本的 Ruby(在撰写本文时),以及最新的兼容版本的 Tcl。
注意:安装 Ruby 2.7.0 后,$ gem install tk
表示 "Tcl/Tk8.6 is not supported[;] it will not work correctly." 因此,我们必须将 Tcl 的使用限制在 8.5 版。我们将通过安装 ActiveTcl 版本 8.5 来完成此操作。
这些步骤适用于 Debian Stretch — 因此,适用于 Fedora 31,YMMV。 :)
创建一些目录:
$ 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 (the Ruby enVironment Manager) by following these steps, according to its 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'"
现在,当我 运行 你的程序时,我看到了一个日历小部件。
我正在尝试使用以下代码在 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(运行时错误)
TkPackage 找不到包 Itk(运行时错误)
我已经安装了 tcl 软件包,但仍然无法正常工作。我在 ruby 2.6.5.
中使用 Fedora 31此答案将安装最新版本的 Ruby(在撰写本文时),以及最新的兼容版本的 Tcl。
注意:安装 Ruby 2.7.0 后,$ gem install tk
表示 "Tcl/Tk8.6 is not supported[;] it will not work correctly." 因此,我们必须将 Tcl 的使用限制在 8.5 版。我们将通过安装 ActiveTcl 版本 8.5 来完成此操作。
这些步骤适用于 Debian Stretch — 因此,适用于 Fedora 31,YMMV。 :)
创建一些目录:
$ 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 (the Ruby enVironment Manager) by following these steps, according to its 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'"
现在,当我 运行 你的程序时,我看到了一个日历小部件。