【问题标题】:Ruby tk: draw while program is in executionRuby tk:在程序执行时绘制
【发布时间】:2023-04-06 22:13:01
【问题描述】:

要显示使用 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

但是,直到游戏结束,所有标记都不会显示在棋盘上。随着游戏的进行,我如何绘制这些标记?

【问题讨论】:

    标签: ruby tk


    【解决方案1】:

    [W]如果我想在应用程序运行时绘制/显示 GUI 的不同部分怎么办?

    [A]在游戏结束之前,所有标记都不会显示在棋盘上。随着游戏的进行,我如何绘制这些标记?

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

    即使在调用 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)有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 2013-01-23
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      相关资源
      最近更新 更多