画布上的按钮。 Ruby Tk

时间:2014-12-08 13:00:52

标签: ruby tk

有没有办法在TkCanavas小部件上放置按钮(TK)?我需要在canavas的不同部分捕捉鼠标按钮动作来做一点TicTacToe游戏。我使用TkCanavas是因为绘制了场线并将对象放在精确坐标上

require 'tk'

class Window

   @@xs = [60,145,235,60,145,235,60,145,235]     
   @@ys = [140,140,140,225,225,225,315,315,315]   #coordinates of circles and crosses

  def run
    root = TkRoot.new { title 'TicTacToe' }
    root.geometry("300x400+300+300")
    img = TkPhotoImage.new(:file => 'cross.gif')
    cv = TkCanvas.new(root)
    cv.place(:height => 400, :width => 300)
    cv.create(TkcImage, @@xs[2], @@ys[2], :image => img)
    cv.create(TkButton, 60, 140,120,200 )  #
    cv.create(TkcLine, 20, 180, 280, 180)  #
    cv.create(TkcLine, 20, 270, 280, 270)  #Field lines
    cv.create(TkcLine, 100, 100, 100, 360) #
    cv.create(TkcLine, 190,100, 190, 360)  #
    TkcText.new(cv, 140, 30, :font => 'Arial 14',
                :text => 'TicTacToe', :anchor => 'center')
    Tk.mainloop
  end
end

1 个答案:

答案 0 :(得分:0)

尝试使用TkcRectangle而不是TkButton。您可以将事件绑定到矩形,就像按钮一样。