ruby tk : draw while program is in execution

标签 ruby tk-toolkit

要显示用 Tk Tk.mainloop 创建的任何元素,必须在程序结束时调用。但是,如果我想在应用程序运行时绘制/显示 GUI 的不同部分怎么办?

更具体地说,我的情况是井字游戏。首先我画板:

def initialize_board
  @root = TkRoot.new { minsize(400, 450) }
  @root.title = "Tic-tac-toe"

  @cv = TkCanvas.new @root
  @cv.place('height' => 350, 'width' => 350, 'x' => 50, 'y' => 100)
  TkcLine.new @cv, 0, 100, 300, 100, width: 5
  TkcLine.new @cv, 0, 200, 300, 200, width: 5
  TkcLine.new @cv, 100, 0, 100, 300, width: 5
  TkcLine.new @cv, 200, 0, 200, 300, width: 5

  @menu = TkMenu.new
  one = TkMenu.new @menu
  @menu.add('cascade', :menu => one, :label => 'Select player')
  one.add('command', :label => 'Human first', :command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) })
  one.add('command', :label => 'Computer first', :command => proc { set_players_and_play(ComputerPlayer, HumanPlayer) })

  @root.menu @menu

  Tk.mainloop
end

然后在控制台中询问用户他想要选择的位置,当他这样做时,标记应该显示在面板中,等等:

def print_board
  (1..9).each do |i|
    position, marker = i, @board[i]
    if marker.eql? "X"
      case position
      when 1
        TkcLine.new @cv, 20, 20, 80, 80, width: 2
        TkcLine.new @cv, 20, 80, 80, 20, width: 2
        #Tk.mainloop
      end
    elsif marker == "O"
    end
  end
end

然而,直到游戏结束,所有的标记都不会显示在棋盘上。如何在游戏进行时绘制这些标记?

最佳答案

[W]hat if I want to draw/display different sections of the GUI as the application goes on?

[A]ll the markers are not displayed on the board until the games ends. How can I draw these markers as the game goes on?

答案已经在您的代码中了!

即使在调用 Tk.mainloop 之后,任何所需的 Ruby 代码仍然可以运行,如果它在回调中的话。

您的代码片段 :command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) } 包含一个 proc 文字。 proc 是 Tk 回调的一个例子。您的代码片段指示 Tk GUI 系统在选择菜单项时调用该 proc

因此,如您所见,菜单项具有回调。许多其他 Tk 小部件也这样做。

我在您的代码中添加了一些行以避免运行时错误(并注明了我在哪里做的)。现在它在第一个位置绘制一个“X”(当从菜单中选择“计算机优先”时):

require 'tk' # Added this line.

def initialize_board
  @root = TkRoot.new { minsize(400, 450) }
  @root.title = "Tic-tac-toe"

  @cv = TkCanvas.new @root
  @cv.place('height' => 350, 'width' => 350, 'x' => 50, 'y' => 100)
  TkcLine.new @cv, 0, 100, 300, 100, width: 5
  TkcLine.new @cv, 0, 200, 300, 200, width: 5
  TkcLine.new @cv, 100, 0, 100, 300, width: 5
  TkcLine.new @cv, 200, 0, 200, 300, width: 5

  @menu = TkMenu.new
  one = TkMenu.new @menu
  @menu.add('cascade', :menu => one, :label => 'Select player')
  one.add('command', :label => 'Human first', :command => proc { set_players_and_play(HumanPlayer, ComputerPlayer) })
  one.add('command', :label => 'Computer first', :command => proc { set_players_and_play(ComputerPlayer, HumanPlayer) })

  @root.menu @menu

  Tk.mainloop
end

def print_board
  (1..9).each do |i|
    position, marker = i, @board[i]
    if marker.eql? "X"
      case position
      when 1
        TkcLine.new @cv, 20, 20, 80, 80, width: 2
        TkcLine.new @cv, 20, 80, 80, 20, width: 2
        #Tk.mainloop
      end
    elsif marker == "O"
    end
  end
end

# Added these lines, below:

HumanPlayer = 'Human player'
ComputerPlayer = 'Computer player'

size = 3 * 3
@board = Array.new size + 1, ''

def set_players_and_play(player_first, player_second)
  puts "#{player_first} first, then #{player_second}"
  if ComputerPlayer == player_first
    @board[1] = 'X'
    print_board
  end
end

initialize_board
print_board

这对我在 Windows 7 上使用 Ruby 2.2.5(带有 Tk 8.5.12)很有效。

关于 ruby tk : draw while program is in execution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27407887/

相关文章:

windows - 当窗口改变位置时如何执行特定代码?

python - tkinter - 显示帧后运行函数

ruby-on-rails - Ruby on Rails 搜索方法

mysql - 循环遍历 Rails 中的多个属性

ruby - 在 ruby​​ 中使用词形还原器

python - 如何使 Tkinter 条目焦点验证不仅仅在第一次发生?

python - 如何在 tkinter text =""属性中包含变量名称?

Ruby 对象批量赋值

ruby - 我是否在单元测试中滥用 stub ?

python - 通过 Tk 小部件/窗口路径名访问小部件