Skip to main content
 首页 » 编程设计

python之如何通过使用 [n] 值的按钮更改 [n] 的值

2025年04月02日17haluo1

好的,我正在为学校做一个项目,即制作 Connect 4,重点是使用顶部的按钮根据转弯设置标签的颜色。我很难过,我无法设置底部的颜色,除非我将 n 值设置为 [n+35]。到目前为止,这是我的代码。另外,我仍然没有适当的获胜条件,我稍后再做。现在我正在寻找有关如何设置标签以正确点亮的帮助。

提前感谢您的帮助。 (抱歉,到目前为止我还是个编码菜鸟。)

from Tkinter import * 
from functools import partial 
 
top = Tk() 
 
top.title("Juan's Zany Super Crazy Connect Four!") 
 
turn=1 
 
def click(n): 
    global turn 
    if turn==1: 
        board[35+n].config(state=DISABLED, bg='blue') 
        turn=turn+1 
    elif turn==2: 
        board[35+n].config(state=DISABLED, bg='red') 
        turn=turn-1 
 
top = Tk() 
r=0 
count=0 
col=0 
 
buttonList = list() 
for i in range(7): 
    buttonList.append(Button(text=str(i), font='Helvetica 48', command=partial(click, i))) 
    buttonList[-1].grid(row=r,column=col, sticky='NESW') 
    count+=1 
    col+=1 
 
r=1 
col=0 
count=0 
 
board = list() 
 
for i in range(42): 
    board.append(Label(text=(i), font='Helvetica 15', bg='grey80')) 
    board[-1].grid(row=r,column=col, sticky='NESW', padx=2, pady=2) 
    count+=1 
    col+=1 
    if count==7: 
        r=r+1 
        count=0 
        col=0 
 
 
top.mainloop() 

更新(2016 年 4 月 5 日):

from Tkinter import * 
from functools import partial 
import time 
 
#time.sleep() 
 
top = Tk() 
 
top.title("Juan's Zany Super Crazy Connect Four!") 
 
turn=1 
 
nextS=[35,36,37,38,39,40,41] 
 
def buttonStuff(z): 
    global turn 
    if turn==1: 
        board[nextS[z]].config(bg='blue') 
        nextS[z]=nextS[z]-7 
        turn=turn+1 
        if nextS[z]<0: 
            buttonList[z].config(state=DISABLED) 
 
    elif turn==2: 
        board[nextS[z]].config(bg='red') 
        nextS[z]=nextS[z]-7 
        turn=turn-1 
        if nextS[z]<0: 
            buttonList[z].config(state=DISABLED) 
 
def checkWin(): 
    for h in range (0,4): 
        if board[35+h].cget('bg')==board[36+h].cget('bg')==board[37+h].cget('bg')==board[38+h].cget('bg')!=bg=='yellow': 
            for q in range (0,4): 
                board[35+h].config(bg='green') 
                board[36+h].config(bg='green') 
                board[37+h].config(bg='green') 
                board[38+h].config(bg='green') 
                time.sleep(0.5) 
                if turn==1: 
                    board[35+h].config(bg='blue') 
                    board[36+h].config(bg='blue') 
                    board[37+h].config(bg='blue') 
                    board[38+h].config(bg='blue') 
                if turn==2: 
                    board[35+h].config(bg='red') 
                    board[36+h].config(bg='red') 
                    board[37+h].config(bg='red') 
                    board[38+h].config(bg='red') 
 
 
 
top = Tk() 
r=0 
count=0 
col=0 
 
buttonList = list() 
for z in range(7): 
    buttonList.append(Button(text=str(z), font='times 48', command=partial(buttonStuff, z))) 
    buttonList[-1].grid(row=r,column=col, sticky='NESW') 
    count+=1 
    col+=1 
 
r=1 
col=0 
count=0 
 
board = list() 
for z in range(42): 
        board.append(Label(text='', font='Helvetica 15', bg='grey80')) 
        board[-1].grid(row=r,column=col, sticky='NESW', padx=2, pady=2, ipadx=2, ipady=25) 
        count+=1 
        col+=1 
        if count==7: 
            r=r+1 
            count=0 
            col=0 
 
 
top.mainloop() 

我想要完成的是现在只让一行(底行)工作,我希望它根据回合/连续“捕获”这 4 个的玩家闪烁蓝色/红色。只是有点挣扎,所以如果你们中的一个人可以快速看一下。另外,请保持这样的基本状态,因为我想了解我做错了什么。

提前谢谢你。

请您参考如下方法:

像这样更改您的click 函数:

def click(n): 
    global turn 
    offset = 0 
    while board[35 + n - offset]['bg'] in {'blue', 'red'}: 
        offset += 7 
    if turn==1: 
        board[35+n-offset].config(state=DISABLED, bg='blue') 
        turn=turn+1 
    elif turn==2: 
        board[35+n-offset].config(state=DISABLED, bg='red') 
        turn=turn-1 

如果您需要更多帮助,请随时提出!