【问题标题】:Tkinter Can't Call Function ( TicTacToe in tkinter)Tkinter 无法调用函数(tkinter 中的 TicTacToe)
【发布时间】:2018-05-27 06:15:04
【问题描述】:

我的代码有问题。所以我正在尝试制作一个井字游戏,但我希望计算机选择一个盒子来放置他的标记。我已经完成了用户可以放置标记的代码,但我无法解决它没有为计算机标记选择框的问题。你可以在这里看到我的代码。 Y1, Y2, Y3.... 等都是当您单击按钮时它会更改它的所有功能。 C1, C2, C3....等都是电脑用来放他的marker的代码。

import tkinter as tk
from tkinter import messagebox
import random

one_flag= True
two_flag= True
three_flag= True
four_flag= True
five_flag= True
six_flag= True
seven_flag= True
eight_flag= True
nine_flag= True
pk = tk.Tk()
frame = tk.Frame(pk)
frame.grid()
def X():
    print("test")
def Y1():
    global one_flag
    if one_flag:
        print("Your Marker Is Now On This Square")
        one.config(text="X",fg='red')
        one_flag = False
def Y2():
    global two_flag
    if two_flag:
        print("Your Marker Is Now On This Square")
        two.config(text="X",fg='red')
        two_flag = False
def Y3():
    global three_flag
    if three_flag:
        print("Your Marker Is Now On This Square")
        three.config(text="X",fg='red')
        three_flag = False
def Y4():
    global four_flag
    if four_flag:
        print("Your Marker Is Now On This Square")
        four.config(text="X",fg='red')
        four_flag = False
def Y5():
    global five_flag
    if five_flag:
        print("Your Marker Is Now On This Square")
        five.config(text="X",fg='red')
        five_flag = False
def Y6():
    global six_flag
    if six_flag:
        print("Your Marker Is Now On This Square")
        six.config(text="X",fg='red')
        six_flag = False
def Y7():
    global seven_flag
    if seven_flag:
        print("Your Marker Is Now On This Square")
        seven.config(text="X",fg='red')
        seven_flag = False
def Y8():
    global eight_flag
    if eight_flag:
        print("Your Marker Is Now On This Square")
        eight.config(text="X",fg='red')
        eight_flag = False
def Y9():
    global nine_flag
    if nine_flag:
        print("Your Marker Is Now On This Square")
        nine.config(text="X",fg='red')
        nine_flag = False
def C1():
    global one_flag
    if one_flag:
        print("The Computer's Marker Is Now On This Square")
        one.config(text="O",fg='blue')
        one_flag = False
def C2():
    global two_flag
    if two_flag:
        print("The Computer's Marker Is Now On This Square")
        two.config(text="O",fg='blue')
        two_flag = False
def C3():
    global three_flag
    if three_flag:
        print("The Computer's Marker Is Now On This Square")
        three.config(text="O",fg='blue')
        three_flag = False
def C4():
    global four_flag
    if four_flag:
        print("The Computer's Marker Is Now On This Square")
        four.config(text="O",fg='blue')
        four_flag = False
def C5():
    global five_flag
    if five_flag:
        print("The Computer's Marker Is Now On This Square")
        five.config(text="O",fg='blue')
        five_flag = False
def C6():
    global six_flag
    if six_flag:
        print("The Computer's Marker Is Now On This Square")
        six.config(text="O",fg='blue')
        six_flag = False
def C7():
    global seven_flag
    if seven_flag:
        print("The Computer's Marker Is Now On This Square")
        seven.config(text="O",fg='blue')
        seven_flag = False
def C8():
    global eight_flag
    if eight_flag:
        print("The Computer's Marker Is Now On This Square")
        eight.config(text="O",fg='blue')
        eight_flag = False
def C9():
    global nine_flag
    if nine_flag:
        print("The Computer's Marker Is Now On This Square")
        nine.config(text="O",fg='blue')
        nine_flag = False

one=tk.Button(pk,
            text="1",
            fg="blue",
            command=Y1)
two=tk.Button(pk,
            text="2",
            fg="blue",
            command=Y2)
three=tk.Button(pk,
            text="3",
            fg="blue",
            command=Y3)
four=tk.Button(pk,
            text="4",
            fg="blue",
            command=Y4)
five=tk.Button(pk,
            text="5",
            fg="blue",
            command=Y5)
six=tk.Button(pk,
            text="6",
            fg="blue",
            command=Y6)
seven=tk.Button(pk,
            text="7",
            fg="blue",
            command=Y7)
eight=tk.Button(pk,
            text="8",
            fg="blue",
            command=Y8)
nine=tk.Button(pk,
            text="9",
            fg="blue",
            command=Y9)

one.grid(row=0,column=0)
two.grid(row=0,column=1)
three.grid(row=0,column=2)
four.grid(row=1,column=0)
five.grid(row=1,column=1)
six.grid(row=1,column=2)
seven.grid(row=2,column=0)
eight.grid(row=2,column=1)
nine.grid(row=2,column=2)


pk.mainloop()
ho=one_flag,two_flag,three_flag,four_flag,five_flag,six_flag,seven_flag,eight_flag,nine_flag

boxes=['one','two','three','four','five','six','seven','eight','nine']
if one_flag==False:
    boxes.remove('one')
if two_flag==False:
    boxes.remove('two')
if three_flag==False:
    boxes.remove('three')
if four_flag==False:
    boxes.remove('four')
if five_flag==False:
    boxes.remove('five')
if six_flag==False:
    boxes.remove('six')
if seven_flag==False:
    boxes.remove('seven')
if eight_flag==False:
    boxes.remove('eight')
if nine_flag==False:
    boxes.remove('nine')

cs=random.choice(boxes)
if cs=='one':
    C1()
if cs=='two':
    C2()
if cs=='three':
    C3()
if cs=='four':
    C4()
if cs=='five':
    C5()
if cs=='six':
    C6()
if cs=='seven':
    C7()
if cs=='eight':
    C8()
if cs=='nine':
    C9()
print(boxes)

【问题讨论】:

  • pk.mainloop()之后的所有代码只有在你关闭窗口后才会执行。您应该将该代码包装在一个函数中,并在用户每次单击其中一个按钮时调用它。
  • 这里有很多 很多 重复,这违背了软件设计的DRY 原则。每当您有一堆编号的变量时,这表明您可能应该使用列表或元组之类的集合。如果您使用 _flag 变量执行此操作,您应该能够用单个函数替换所有 Y 函数,对于那些 C 函数也是如此。同样,您可以将所有按钮循环并存储在列表中。
  • Aran-Fey 谢谢你的建议,但我该怎么做呢?
  • 你真的需要九个按钮来说明你的问题吗?请创建一个minimal reproducible example。因为问题出在调用命令的按钮上,所以您的示例中可能只需要一个按钮。
  • Bryan Oakley-当然是我的错误。重点是你帮助我,所以谢谢你。我会用 1 个按钮向你展示,但你需要帮助我了解如何调用计算机功能

标签: python function loops tkinter


【解决方案1】:

既然你是importing random,所以我想你想让电脑随机选择一个框。我已经编辑并改进了您的代码。

变化:

  • 随机电脑盒选择。
  • 空闲框的颜色为green,而电脑选中按钮的颜色为blue
  • 无法再次单击计算机选择的按钮。
  • 逼真的计算机按钮点击

随机选择的工作:

每次我们点击一​​个按钮,下一轮是电脑。所以当我们点击一​​个按钮时,它会运行一个函数。该函数以[True,True,False.....] 的形式列出所有按钮。之后出现while True 循环,并在从09 的for 循环中从["0","1"] 中随机选择一个项目。如果输出是1,那么它是returns 项目的索引(循环的当前值)。之后,它会查看选择了哪个计算机的随机按钮。 if returned == 0: 运行C1(),if returned == 1: 运行C2() 等等。

代码:

import tkinter as tk
from tkinter import messagebox
import random

one_flag= True
two_flag= True
three_flag= True
four_flag= True
five_flag= True
six_flag= True
seven_flag= True
eight_flag= True
nine_flag= True
pk = tk.Tk()
frame = tk.Frame(pk)
frame.grid()
def pressed(button):
    button["relief"] = tk.SUNKEN
    pk.after(100,func = lambda : button.config(relief= tk.RAISED,state="disabled"))
def select_r():
    try:del avalist
    except:pass
    avalist = [one_flag,two_flag,three_flag,four_flag,five_flag,six_flag,seven_flag,eight_flag,nine_flag]
    print(avalist)
    while True:
        for x in range(0,9):
            toutput = random.choice(["0","1"])
            if toutput == "1" and avalist[x] != False:return x
            y = 0
            for a in avalist:
                if a == False:y += 1
            if y == 9:return None
def select_r2():
    option = select_r()
    if option == 0:C1()
    if option == 1:C2()
    if option == 2:C3()
    if option == 3:C4()
    if option == 4:C5()
    if option == 5:C6()
    if option == 6:C7()
    if option == 7:C8()
    if option == 8:C9()
def X():
    print("test")
def Y1():
    global one_flag
    if one_flag:
        print("Your Marker Is Now On This Square")
        one.config(text="X",fg='red')
        one_flag = False
        select_r2()
def Y2():
    global two_flag
    if two_flag:
        print("Your Marker Is Now On This Square")
        two.config(text="X",fg='red')
        two_flag = False
        select_r2()
def Y3():
    global three_flag
    if three_flag:
        print("Your Marker Is Now On This Square")
        three.config(text="X",fg='red')
        three_flag = False
        select_r2()
def Y4():
    global four_flag
    if four_flag:
        print("Your Marker Is Now On This Square")
        four.config(text="X",fg='red')
        four_flag = False
        select_r2()
def Y5():
    global five_flag
    if five_flag:
        print("Your Marker Is Now On This Square")
        five.config(text="X",fg='red')
        five_flag = False
        select_r2()
def Y6():
    global six_flag
    if six_flag:
        print("Your Marker Is Now On This Square")
        six.config(text="X",fg='red')
        six_flag = False
        select_r2()
def Y7():
    global seven_flag
    if seven_flag:
        print("Your Marker Is Now On This Square")
        seven.config(text="X",fg='red')
        seven_flag = False
        select_r2()
def Y8():
    global eight_flag
    if eight_flag:
        print("Your Marker Is Now On This Square")
        eight.config(text="X",fg='red')
        eight_flag = False
        select_r2()
def Y9():
    global nine_flag
    if nine_flag:
        print("Your Marker Is Now On This Square")
        nine.config(text="X",fg='red')
        nine_flag = False
        select_r2()
def C1():
    global one_flag
    if one_flag:
        print("The Computer's Marker Is Now On This Square")
        one.config(text="O",fg='blue')
        pressed(one)
        one_flag = False
def C2():
    global two_flag
    if two_flag:
        print("The Computer's Marker Is Now On This Square")
        two.config(text="O",fg='blue')
        pressed(two)
        two_flag = False
def C3():
    global three_flag
    if three_flag:
        print("The Computer's Marker Is Now On This Square")
        three.config(text="O",fg='blue')
        pressed(three)
        three_flag = False
def C4():
    global four_flag
    if four_flag:
        print("The Computer's Marker Is Now On This Square")
        four.config(text="O",fg='blue')
        pressed(four)
        four_flag = False
def C5():
    global five_flag
    if five_flag:
        print("The Computer's Marker Is Now On This Square")
        five.config(text="O",fg='blue')
        pressed(five)
        five_flag = False
def C6():
    global six_flag
    if six_flag:
        print("The Computer's Marker Is Now On This Square")
        six.config(text="O",fg='blue')
        pressed(six)
        six_flag = False
def C7():
    global seven_flag
    if seven_flag:
        print("The Computer's Marker Is Now On This Square")
        seven.config(text="O",fg='blue')
        pressed(seven)
        seven_flag = False
def C8():
    global eight_flag
    if eight_flag:
        print("The Computer's Marker Is Now On This Square")
        eight.config(text="O",fg='blue')
        pressed(eight)
        eight_flag = False
def C9():
    global nine_flag
    if nine_flag:
        print("The Computer's Marker Is Now On This Square")
        nine.config(text="O",fg='blue')
        pressed(nine)
        nine_flag = False

one=tk.Button(pk,
            text="1",
            fg='green',
            command=Y1)
two=tk.Button(pk,
            text="2",
            fg='green',
            command=Y2)
three=tk.Button(pk,
            text="3",
            fg='green',
            command=Y3)
four=tk.Button(pk,
            text="4",
            fg='green',
            command=Y4)
five=tk.Button(pk,
            text="5",
            fg='green',
            command=Y5)
six=tk.Button(pk,
            text="6",
            fg='green',
            command=Y6)
seven=tk.Button(pk,
            text="7",
            fg='green',
            command=Y7)
eight=tk.Button(pk,
            text="8",
            fg='green',
            command=Y8)
nine=tk.Button(pk,
            text="9",
            fg='green',
            command=Y9)

one.grid(row=0,column=0)
two.grid(row=0,column=1)
three.grid(row=0,column=2)
four.grid(row=1,column=0)
five.grid(row=1,column=1)
six.grid(row=1,column=2)
seven.grid(row=2,column=0)
eight.grid(row=2,column=1)
nine.grid(row=2,column=2)


pk.mainloop()

动画:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多