2017-10-17 96 views
1

我正在用tkinter编写一个简单的国际象棋游戏,目的是通过单击开始和结束方块(选择坐标要移动的棋子以及目标的坐标)。不过,我似乎无法更新按钮的文字以显示作品已移动。我已经查看了一些以前的答案/解决方案,但是大多数要求每个按钮被单独更改/选择,这是我无法做到的,因为电路板(可视化表示)是一组8个按钮。如何更新用于象棋游戏的tkinter按钮的文本

大多数推荐使用tkinters的StringVar(),但我似乎无法得到它的工作。

我想我的问题是如何在移动后更新按钮。

['WR', 'WN', 'WB', 'WK', 'WQ', 'WB', 'WN', 'WR'] row 1 
['WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP', 'WP'] 
[None, None, None, None, None, None, None, None] 
[None, None, None, None, None, None, None, None] 
[None, None, None, None, None, None, None, None] 
[None, None, None, None, None, None, None, None] 
['BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP', 'BP'] 
['BR', 'BN', 'BB', 'BK', 'BQ', 'BB', 'BN', 'BR'] row 7 

Board_visual是建立在游戏的2D列表看起来像这样(上图):

import game 
from tkinter import * 
from functools import partial 
first=True 

main=Tk() 
main.title("TEST") 
main.geometry("1000x1000") 
buttonframe=Frame(main) 
buttonframe.grid(row=0,column=0) 
index=0 
colour="white" 
for i in game.board_visual: 
    index2=0 
    for j in i: 
     if j==None: 
      j="" 
     buttontext=StringVar() 
     buttontext.set(j) 
     Button(buttonframe, textvariable=buttontext,fg="red",command=lambda row=index,column=index2: move(row,column),font=30, width=10,height=2,bg=colour,).grid(row=index, column=index2, sticky=W) 
     if colour=="white": 
      colour="black" 
     elif colour=="black": 
      colour="white" 
     index2=index2+1 
    if colour=="white": 
     colour="black" 
    elif colour=="black": 
     colour="white" 
    index=index+1 

def move(row,column): 
    global first 
    if first==True: 
     first=False 
     global startx 
     global starty 
     startx=row 
     starty=column 
    else: 
     endx=row 
     endy=column 
     first=True 
     game.board[startx][starty].move(endx,endy) 
     game.update(game.board,game.board_visual) 

     update() 
def update(): 
    index=0 
    for i in game.board_visual: 
     index2=0 
     for j in i: 
      buttontext.set(j) 
      index2=index2+1 
    index=index+1 
main.mainloop() 

使用全局运行startx和starty的让我来存储第一的坐标按钮压出主循环的一侧。我已经这样做了,所以当第二个按钮被按下时,我有两套移动功能的坐标。

我仍然是新来的python和编程的一般,所以我不知道我在做什么错了。我非常感谢你能给我的任何帮助。这也是我第一次发布一个问题,所以我已经阅读了建议,所以我希望我在正确的地方。如果这个问题有问题,请告诉我,我会尝试纠正它。我应该删减一些不相关的代码吗?例如导致按钮颜色从白色变为黑色的位。

+0

简短的回答是,这是可能的。您很可能需要使用带有自引用调用的3D按钮阵列,然后检查另一侧的“文本”值以确定所需的操作。但对我而言,这听起来像是对tkinter库的滥用,它并不是真正为这种事情设计的,并且还有更适合用于游戏设计目的的Python库,例如[Pygame](https:// www .pygame.org /新闻)。使用替代库时,您可能会有更多,更容易的时间。 –

+0

谢谢,我不会说这是我期待的答案,但我并不感到惊讶。我会看看pygame。再次感谢 –

回答

0

我发现的一种方法是使按钮创建一个过程,并在每一步之后调用它。这工作,但我相信它只是重新创建按钮,而不是改变它们,这可能是一个不好的解决方案,但它的工作。如果有人发现它有帮助,我会在这里发布代码更改。

def button_text_change(): 
    index=0 
    colour="white" 
    for i in game.board_visual: 
     index2=0 
     for j in i: 
      if j==None: 
       j="" 
      buttontext=StringVar() 
      buttontext.set(j) 
      Button(buttonframe, textvariable=buttontext,fg="red",command=lambda row=index,column=index2: move(row,column),font=30, width=10,height=2,bg=colour,).grid(row=index, column=index2, sticky=W) 
      if colour=="white": 
       colour="black" 
      elif colour=="black": 
       colour="white" 
      index2=index2+1 
     if colour=="white": 
      colour="black" 
     elif colour=="black": 
      colour="white" 
     index=index+1