<legend id='MapNh'><style id='MapNh'><dir id='MapNh'><q id='MapNh'></q></dir></style></legend>

        <i id='MapNh'><tr id='MapNh'><dt id='MapNh'><q id='MapNh'><span id='MapNh'><b id='MapNh'><form id='MapNh'><ins id='MapNh'></ins><ul id='MapNh'></ul><sub id='MapNh'></sub></form><legend id='MapNh'></legend><bdo id='MapNh'><pre id='MapNh'><center id='MapNh'></center></pre></bdo></b><th id='MapNh'></th></span></q></dt></tr></i><div id='MapNh'><tfoot id='MapNh'></tfoot><dl id='MapNh'><fieldset id='MapNh'></fieldset></dl></div>
          <bdo id='MapNh'></bdo><ul id='MapNh'></ul>
      1. <tfoot id='MapNh'></tfoot>

        <small id='MapNh'></small><noframes id='MapNh'>

        使用 Tkinter 创建游戏板

        Creating a game board with Tkinter(使用 Tkinter 创建游戏板)
            <tbody id='hbrBh'></tbody>

              <small id='hbrBh'></small><noframes id='hbrBh'>

              • <bdo id='hbrBh'></bdo><ul id='hbrBh'></ul>
                1. <tfoot id='hbrBh'></tfoot>
                  <i id='hbrBh'><tr id='hbrBh'><dt id='hbrBh'><q id='hbrBh'><span id='hbrBh'><b id='hbrBh'><form id='hbrBh'><ins id='hbrBh'></ins><ul id='hbrBh'></ul><sub id='hbrBh'></sub></form><legend id='hbrBh'></legend><bdo id='hbrBh'><pre id='hbrBh'><center id='hbrBh'></center></pre></bdo></b><th id='hbrBh'></th></span></q></dt></tr></i><div id='hbrBh'><tfoot id='hbrBh'></tfoot><dl id='hbrBh'><fieldset id='hbrBh'></fieldset></dl></div>
                  <legend id='hbrBh'><style id='hbrBh'><dir id='hbrBh'><q id='hbrBh'></q></dir></style></legend>

                2. 本文介绍了使用 Tkinter 创建游戏板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试构建一个简单的 Connect FourPython(2.7)

                  I am trying to build a simple game of Connect Four with Python(2.7)

                  我创建了一个板,它由一个简单的多维 Python 列表组成.
                  我的董事会列表如下所示:

                  I have created a board, that consists of a simple multidimensional Python list.
                  My Board list looks like this:

                  board = [
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,_,_,_,_,_,_],
                      [_,_,_,_,O,_,_,_,_,_],
                      [_,_,_,_,X,_,_,_,_,_],
                      [_,_,_,_,X,O,_,_,_,_],
                      [_,_,_,_,X,O,_,_,_,_],
                  ]
                  

                  如果 X 是 Player1 而 O 是 Player2(或计算机).

                  Were X is Player1 and O is Player2 (or Computer).

                  现在,我为 GUI 创建了一些基本代码,如下所示:

                  Now, I have created some basic code for the GUI, like this:

                  # Connect 4 Game
                  import Tkinter
                  
                  screen = Tkinter.Tk()
                  screen.title("My First Game")
                  
                  #Create a board
                  board = Tkinter.Canvas(screen,width=500,height=500)
                  board.pack()
                  
                  screen.mainloop()
                  

                  问题:我怎样才能创建板的视觉表示,以便为每个列表,有一个矩形?另外,有没有办法检测,当一个矩形被点击并替换相应的列表值?

                  Question: How can i create a visual representation of the board, so that for every list, there is a rectangle? Also, is there a way to detect, when a rectangle is clicked and replace the corresponding list value?

                  推荐答案

                  我创建了一个标签板并根据点击的颜色为它们着色:

                  I created a board of labels and color them according to which is clicked:

                  import Tkinter as tk
                  
                  board = [ [None]*10 for _ in range(10) ]
                  
                  counter = 0
                  
                  root = tk.Tk()
                  
                  def on_click(i,j,event):
                      global counter
                      color = "red" if counter%2 else "black"
                      event.widget.config(bg=color)
                      board[i][j] = color
                      counter += 1
                  
                  
                  for i,row in enumerate(board):
                      for j,column in enumerate(row):
                          L = tk.Label(root,text='    ',bg='grey')
                          L.grid(row=i,column=j)
                          L.bind('<Button-1>',lambda e,i=i,j=j: on_click(i,j,e))
                  
                  root.mainloop()
                  

                  这不会进行任何验证(例如,确保单击的元素位于底部).使用类而不是全局数据也会更好,但这对于感兴趣的编码人员来说是一个练习:).

                  This doesn't do any validation (to make sure that the element clicked is at the bottom for example). It would also be much better with classes instead of global data, but that's an exercise for the interested coder :).

                  这篇关于使用 Tkinter 创建游戏板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <i id='HSLac'><tr id='HSLac'><dt id='HSLac'><q id='HSLac'><span id='HSLac'><b id='HSLac'><form id='HSLac'><ins id='HSLac'></ins><ul id='HSLac'></ul><sub id='HSLac'></sub></form><legend id='HSLac'></legend><bdo id='HSLac'><pre id='HSLac'><center id='HSLac'></center></pre></bdo></b><th id='HSLac'></th></span></q></dt></tr></i><div id='HSLac'><tfoot id='HSLac'></tfoot><dl id='HSLac'><fieldset id='HSLac'></fieldset></dl></div>

                    <legend id='HSLac'><style id='HSLac'><dir id='HSLac'><q id='HSLac'></q></dir></style></legend>

                    <small id='HSLac'></small><noframes id='HSLac'>

                            <bdo id='HSLac'></bdo><ul id='HSLac'></ul>
                            <tfoot id='HSLac'></tfoot>
                              <tbody id='HSLac'></tbody>