python - How to make a shape change color when I click on it in a Connect Four board? -


i'm writing connect 4 game in python using tkinter. i'm making board. want circles change color when click them.

only last column of last row changed wherever click on board. how can make whenever click specific circle, circle changes color?

from tkinter import * import random  def conx_create_window():     mw = tk()     mw.title("connect 4 game")     mw.geometry("650x600")     mw.configure(bg="#3c3c3c", padx=50, pady=50)     return mw  def main():     m_window = conx_create_window()     return m_window  m_window = main() mframe = frame(m_window, bg="#3c3c3c", padx=50, pady=150) mframe.pack() newframe = frame(m_window, bg="#3c3c3c", padx=50, pady=50) board = {} buttons = {} frames = {} gameboard = frame(m_window)  #----------------------------------  def newgame_click():     print("new game")     mframe.pack_forget()     boardoption()  def boardoption():     newframe.pack()  def board7x6():     gameboard.pack()     newframe.pack_forget()     print("7x6 board size")     row in range(6):         board[row] = {}         frames[row] = frame(gameboard)         frames[row].pack()         col in range(7):             board[row][col] = 0             frame = frame(frames[row])             frame.pack(side = left)             c = canvas(frame, bg="#666", width=50, height=50)             c.pack()             r = c.create_rectangle((0, 0, 50, 50), fill="#3c3c3c")             circle = c.create_oval(3, 3, 49, 49, fill="#3d3d3d")             c.tag_bind(r, "<button-1>", lambda event: print('works'))             c.tag_bind(circle, "<button-1>", lambda event: c.itemconfig(circle, fill="green"))             print(" ", board[row][col], " ", end="")         print()  def board8x7():     gameboard.pack()     newframe.pack_forget()     print("8x7 board size")     row in range(7): # 7 rows         board[row] = {}         buttons[row] = {}         frames[row] = frame(gameboard)         frames[row].pack()         col in range(8): # 8 columns             board[row][col] = 0             buttons[row][col] = button(frames[row], text="", width=8, height=4, bg="#1ec811", bd=0, highlightthickness=0)             print("     ", board[row][col], "     ", end="")             buttons[row][col].pack(side=left)         print()  board7x6_btn = button(newframe, text="7x6", bg="#64e545", command=board7x6, bd=0, highlightthickness=0) board8x7_btn = button(newframe, text="8x7", bg="#64e545", command=board8x7, bd=0, highlightthickness=0)  board7x6_btn.grid(row=0, column=0, padx=20, pady=10, ipadx=20, ipady=20) board8x7_btn.grid(row=0, column=1, padx=20, pady=10, ipadx=20, ipady=20)  newgame_btn = button(mframe, text="new game", bg="#64e545", command=newgame_click, bd=0, highlightthickness=0) load_btn = button(mframe, text="load", bg="#64e545", padx=25, bd=0, highlightthickness=0) ins_btn = button(mframe, text="instructions", bg="#64e545", bd=0, highlightthickness=0) exit_btn = button(mframe, text="exit", bg="#64e545", padx=10, bd=0, highlightthickness=0)#, command = exit_click)  newgame_btn.grid(row=0, column=0, padx=10, pady=10, ipadx=10, ipady=20) load_btn.grid(row=0, column=1, padx=10, pady=10, ipady=20) ins_btn.grid(row=1, column=0, padx=10, pady=10, ipady=20) exit_btn.grid(row=1, column=1, padx=10, pady=10, ipadx=20, ipady=20)  #----------------------------------  m_window.mainloop() 

the problem lambda construction: c same (last one) , therefore not evaluated @ execution:

c.tag_bind(circle, '<button-1>',  lambda event: c.itemconfig(circle, fill = "green")) 

use default argument instead::

c.tag_bind(circle, '<button-1>',  lambda event, c=c: c.itemconfig(circle, fill = "green")) 

so c default argument , have different lamdas. see here far better explanation mine.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -