Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有一个问题,我无法在tkinter中设置带条件的按钮 从tkinter导入* 随机输入 def单击(按钮): 全局单击 全球球员得分 全局cpu_分数 全球胜利 全球btn_列表 全球转向 单击=随机选择(btn\U列表) 回合_Python_User Interface_Tkinter - Fatal编程技术网

Python 我有一个问题,我无法在tkinter中设置带条件的按钮 从tkinter导入* 随机输入 def单击(按钮): 全局单击 全球球员得分 全局cpu_分数 全球胜利 全球btn_列表 全球转向 单击=随机选择(btn\U列表) 回合

Python 我有一个问题,我无法在tkinter中设置带条件的按钮 从tkinter导入* 随机输入 def单击(按钮): 全局单击 全球球员得分 全局cpu_分数 全球胜利 全球btn_列表 全球转向 单击=随机选择(btn\U列表) 回合,python,user-interface,tkinter,Python,User Interface,Tkinter,当我运行并单击其中任何一个按钮时,就会出现错误消息。我想将一个玩家设置为电脑,点击随机按钮,然后改变玩家的轮次,那么我可以问一下,为了达到这个效果,我该怎么写?另外,我想问一下,我们什么时候应该在函数中使用lambda?您需要一个局部变量。部分是lambda的一部分。我曾试着读过关于兰姆达的书,但没有一本是很好的。我认为兰姆达是活的。如果我需要将一个变量传递给回调,我会使用lambda声明我需要一个活的(变化的变量)。在这种情况下,您需要一个活动函数,但每个按钮都需要一个静态变量,因此您需要一个

当我运行并单击其中任何一个按钮时,就会出现错误消息。我想将一个玩家设置为电脑,点击随机按钮,然后改变玩家的轮次,那么我可以问一下,为了达到这个效果,我该怎么写?另外,我想问一下,我们什么时候应该在函数中使用lambda?

您需要一个局部变量。部分是lambda的一部分。我曾试着读过关于兰姆达的书,但没有一本是很好的。我认为兰姆达是活的。如果我需要将一个变量传递给回调,我会使用lambda声明我需要一个活的(变化的变量)。在这种情况下,您需要一个活动函数,但每个按钮都需要一个静态变量,因此您需要一个局部变量。在下面的代码中,单击函数中的所有全局变量随着程序的变化而变化,每次按btn1时,它都会将单击函数发送为零,btn2发送为一,依此类推。我继续纠正了您的回调函数和一个使用.after方法调用自身的robot函数。1000是以毫秒为单位的时间,因此您可以相应地进行调整

从tkinter导入*
从functools导入部分
随机输入
def单击(按钮编号):
全球球员得分
全局cpu_分数
全球胜利
全球btn_列表
全球转向
全球玩家
按钮=btn\u列表[按钮数量]
如果按钮['text']='':
按钮。配置(text='O',fg='yellow')
按钮。配置(状态=禁用)
玩家=非玩家
def robot():
全局窗口
全球球员得分
全局cpu_分数
全球胜利
全球玩家
如果不是玩家:
按钮=随机选择(btn\U列表)
如果按钮['text']==“”:
按钮。配置(text='X',fg='green')
按钮。配置(状态=禁用)
玩家=非玩家
如果(btn1['text']='O'和btn2['text']='O'和btn3['text']='O'或
btn4['text']=='O'和btn5['text']=='O'和btn6['text']=='O'或
btn7['text']=='O'和btn8['text']=='O'和btn9['text']=='O'或
btn1['text']=='O'和btn5['text']=='O'和btn9['text']=='O'或
btn3['text']=='O'和btn5['text']=='O'和btn7['text']=='O'或
btn1['text']=='O'和btn4['text']=='O'和btn7['text']=='O'或
btn2['text']=='O'和btn5['text']=='O'和btn8['text']=='O'或
from tkinter import *
import random
            
def click(buttons):
    global click
    global player_score
    global cpu_score
    global win
    global btn_list
    global turn
    click = random.choice(btn_list)
    while turn < 10 and not win:
        if buttons['text'] == ' ' and click == False :
            buttons.configure(text='O',fg='yellow')
            buttons.configure(state=DISABLED)
            click = True
        else:
            buttons = random.choice(btn_list)
            buttons.configure(text='X',fg='green')
            buttons.configure(state=DISABLED)
            click = False
    
    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
click = False
win = False
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "E:/Python/Python Projects Fun/tic-tac-toe_gui.py", line 73, in <lambda>
    btn8 = Button(window,text=' ',bg='blue',height=4,width=8,command=lambda:click(btn8))
TypeError: 'bool' object is not callable