python - 单击按钮时没有反应
问题描述
from tkinter import *
import random
def player_turn():
if buttons['text'] == ' ':
buttons['text'] = 'O'
def cpu_turn():
if buttons['text'] == ' ':
x = random.randint(1,9)
btn_list[x]['text'] = 'X'
def game():
while turns < 10 and not win:
if turns % 2 == 1:
player_turn()
else:
cpu_turn()
def click(buttons):
global clicked
global player_score
global cpu_score
global win
if (btn1['text'] == 'O' and btn2['text'] == 'O' and btn3['text'] == 'O' or
btn4['text'] == 'O' and btn5['text'] == 'O' and btn6['text'] == 'O' or
btn7['text'] == 'O' and btn8['text'] == 'O' and btn9['text'] == 'O' or
btn1['text'] == 'O' and btn5['text'] == 'O' and btn9['text'] == 'O' or
btn3['text'] == 'O' and btn5['text'] == 'O' and btn7['text'] == 'O' or
btn1['text'] == 'O' and btn4['text'] == 'O' and btn7['text'] == 'O' or
btn2['text'] == 'O' and btn5['text'] == 'O' and btn8['text'] == 'O' or
btn3['text'] == 'O' and btn6['text'] == 'O' and btn9['text'] == 'O'):
player_score += 1
win = True
elif (btn1['text'] == 'X' and btn2['text'] == 'X' and btn3['text'] == 'X' or
btn4['text'] == 'X' and btn5['text'] == 'X' and btn6['text'] == 'X' or
btn7['text'] == 'X' and btn8['text'] == 'X' and btn9['text'] == 'X' or
btn1['text'] == 'X' and btn5['text'] == 'X' and btn9['text'] == 'X' or
btn3['text'] == 'X' and btn5['text'] == 'X' and btn7['text'] == 'X' or
btn1['text'] == 'X' and btn4['text'] == 'X' and btn7['text'] == 'X' or
btn2['text'] == 'X' and btn5['text'] == 'X' and btn8['text'] == 'X' or
btn3['text'] == 'X' and btn6['text'] == 'X' and btn9['text'] == 'X'):
cpu_score += 1
win = True
window = Tk()
window.title('Tic Tac Toe')
buttons = StringVar()
btn1 = Button(window,text=' ',bg='red',height=4,width=8,command=lambda:click(btn1))
btn1.grid(row=1,column=0)
btn2 = Button(window,text=' ',bg='blue',height=4,width=8,command=lambda:click(btn2))
btn2.grid(row=1,column=1)
btn3 = Button(window,text=' ',bg='red',height=4,width=8,command=lambda:click(btn3))
btn3.grid(row=1,column=2)
btn4 = Button(window,text=' ',bg='blue',height=4,width=8,command=lambda:click(btn4))
btn4.grid(row=2,column=0)
btn5 = Button(window,text=' ',bg='red',height=4,width=8,command=lambda:click(btn5))
btn5.grid(row=2,column=1)
btn6 = Button(window,text=' ',bg='blue',height=4,width=8,command=lambda:click(btn6))
btn6.grid(row=2,column=2)
btn7 = Button(window,text=' ',bg='red',height=4,width=8,command=lambda:click(btn7))
btn7.grid(row=3,column=0)
btn8 = Button(window,text=' ',bg='blue',height=4,width=8,command=lambda:click(btn8))
btn8.grid(row=3,column=1)
btn9 = Button(window,text=' ',bg='red',height=4,width=8,command=lambda:click(btn9))
btn9.grid(row=3,column=2)
btn_list = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9]
player_score = 0
cpu_score = 0
turn = 1
win = False
当我单击按钮时,没有错误,但没有任何影响。在这里,我想在播放器和 cpu 之间的轮换中设置 while 循环,但我不知道产生这种效果的最佳方法是什么,所以有人可以教我吗?另外,如果按钮已经有文本,那么我该如何设置之后就不能点击了?想问一下lambda函数有什么用,因为不知道什么时候用
解决方案
上面发布的代码将不会运行,因为 tkinter 框架仅在脚本包含 mainloop() 函数时可见,该函数会一直运行直到窗口关闭。
所以在最后添加以下行:
window.mainloop()
此外,由于您正在调用 click() 函数以及传递的按钮,这将是唯一要执行的函数。它不包含对以下三个函数中的任何一个的调用:
game()
cpu_turn()
player_turn()
推荐阅读
- java - 获取R2DBC连接Java Spring失败
- flutter - 使用 Flutter for Mediawiki 进行客户端登录
- python - 如何检测价格何时高于先前的高点
- c# - Serilog 属性解构不能始终如一地工作?
- pandas - 向列添加唯一 id 并在来自同一 pandas 数据帧的唯一 id 中包含一个值
- javascript - 查找行数组中最后一列的最后一个值
- css - 如何使svg文本响应
- python - 使用字符串和 int 错误从一年创建日期 - PYTHON
- laravel - Laravel 模型函数最佳价格
- c++ - 有没有办法显示正在使用的未初始化变量?