2017-01-01 7 views
1

私のコード「TypeError:文字列インデックスは整数でなければなりません」からこのエラーを理解できません。すでにコードに整数を渡す:文字列インデックスはforループからの整数エラーでなければなりませんが、整数を渡しています

def col1_button_click(self, x, y): 
      p = self.players_lst[self.currnt_player_index] 

      for x in range(6,0,-1): 
        if self.buttons_2d_list[x][0]["text"] == self.__space: 
          button = self.buttons_2d_list[x][0] 
          button["text"] = p.get_player_symbol() 
          self.gboard.make_move(x, 0, p.get_player_symbol()) 

          winner = self.gboard.check_winner() # The board will check after each move, if any player has won the game 

          is_full = self.gboard.is_board_full() 

          if winner == True: 
                # Show current player's symbol as Winner, 
                  # and terminate the game 
            win_messge = ("Player %s is the Winner!" % p.get_player_symbol()) 
            messagebox.showinfo("Winner Info ",win_messge) 
            self.mw.destroy() 
            exit() 

          if is_full == True: 
            messagebox.showinfo("Winner Info", "The game ended in a draw!") 
            self.mw.destroy() 
            exit() 
          else: 
            pass 

          if self.currnt_player_index == 1: 
            self.currnt_player_index = 0 
          else: 
            self.currnt_player_index+=1 # increment index by 1 

          p = self.players_lst[self.currnt_player_index] 
          p.play() 

エラーで、このラインから、第四1ダウン:

if self.buttons_2d_list[x][0]["text"] == self.__space: 

が、私の理解から、私はすでにラインより上の範囲から整数を渡していますXの代わりに誰かが私が間違っていた場所を説明できるなら、私はとても感謝しています:)

+0

テキストのような整数インデックスによって、その要素にアクセスする必要があります大丈夫です、それはtkinterボタンのテキストを取得しています。 xを任意の数に変更すると、その範囲がxに渡されていない理由が分かりません。 –

答えて

0

self.buttons_2d_list [X] [0]の文字列でなければなりません... プリントそれは前と見ていただきました、それはあなたがやっている、文字列を返す場合

... 
print(self.buttons_2d_list[x][0]) 
if self.buttons_2d_list[x][0]["text"] == self.__space: 
    ... 

内側:

"string"['text'] 
間違っている

...

+0

何も印刷されず、上記のエラーが返されます。また、私はtkinterを使用していると言及する必要があり、このアクションはボタン・プレスで行われます。 –

+0

空の文字列 "" ... try print(type(self.buttons_2d_list [x] [0])) –

0

self.buttons_2d_list[x][0] 
012の場合

が辞書である、あなたは

self.buttons_2d_list[x][0]["text"] 

を書き込むことによって、キー「テキスト」の値にアクセスすることができますが、それは何か他のものである場合は、

self.buttons_2d_list[x][0][42] 
関連する問題