【发布时间】: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