我正在用Tkinter用Python编写一个连接四的游戏。我现在正在做一个板。我想让圆圈在我点击它们时改变颜色。
只有最后一行中的最后一列在我单击线路板的任何地方被更改。我怎样才能使它使每当我点击一个特定的圆,只有该圆改变颜色?
from tkinter import *
import random
def conx_create_window():
mw = Tk()
mw.title("Connect Four 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")
for row in range(6):
board[row] = {}
frames[row] = Frame(gameBoard)
frames[row].pack()
for 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")
for row in range(7): # 7 rows
board[row] = {}
buttons[row] = {}
frames[row] = Frame(gameBoard)
frames[row].pack()
for 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()
最佳答案
问题是lambda
构造:c始终是相同的(最后一个),因此在执行时不进行评估:
c.tag_bind(circle, '<Button-1>', lambda event: c.itemconfig(circle, fill = "green"))
请改用默认参数:
c.tag_bind(circle, '<Button-1>', lambda event, c=c: c.itemconfig(circle, fill = "green"))
所以
c
现在是默认参数,现在有了不同的lamdas。比我的解释好得多的解释见。关于python - 在“连接四板”中单击时如何使形状改变颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37113520/