Python 战列舰,但无法定义
我一直在为战舰游戏制作GUI,但当代码运行时,我发现错误: A1.配置(卸压=下沉) 名称错误:未定义名称“A1” 我意识到按钮是在Python 战列舰,但无法定义,python,tkinter,Python,Tkinter,我一直在为战舰游戏制作GUI,但当代码运行时,我发现错误: A1.配置(卸压=下沉) 名称错误:未定义名称“A1” 我意识到按钮是在def函数中命名的,所以我将它们设置为global,但是仍然会出现相同的错误。然而,我对global非常陌生,不完全确定我是否正确使用了它 from tkinter import* import random import time ##### main window = Tk() window.title("battleships") # makes ever
def
函数中命名的,所以我将它们设置为global
,但是仍然会出现相同的错误。然而,我对global
非常陌生,不完全确定我是否正确使用了它
from tkinter import*
import random
import time
##### main
window = Tk()
window.title("battleships")
# makes every button global
global A1
global A2
global A3
global B1
global B2
global B3
global C1
global C2
global C3
# command that places the buttons when the ships are placed
def placeships():
A1 = Button(window, width=5, height=2, text="A1", command=clickA1).place(x=0, y=0)
A2 = Button(window, width=5, height=2, text="A2", command=clickA2).place(x=50, y=0)
A3 = Button(window, width=5, height=2, text="A3", command=clickA3).place(x=100, y=0)
B1 = Button(window, width=5, height=2, text="B1", command=clickB1).place(x=0, y=50)
B2 = Button(window, width=5, height=2, text="B2", command=clickB2).place(x=50, y=50)
B3 = Button(window, width=5, height=2, text="B3", command=clickB3).place(x=100, y=50)
C1 = Button(window, width=5, height=2, text="C1", command=clickC1).place(x=0, y=100)
C2 = Button(window, width=5, height=2, text="C2", command=clickC2).place(x=50, y=100)
C3 = Button(window, width=5, height=2, text="C3", command=clickC3).place(x=100, y=100)
# commands for the buttons
def clickA1():
A1.configure(relief=SUNKEN)
def clickA2():
A2.configure(relief=SUNKEN)
def clickA3():
A3.configure(relief=SUNKEN)
def clickB1():
B1.configure(relief=SUNKEN)
def clickB2():
B2.configure(relief=SUNKEN)
def clickB3():
B3.configure(relief=SUNKEN)
def clickC1():
C1.configure(relief=SUNKEN)
def clickC2():
C2.configure(relief=SUNKEN)
def clickC3():
C3.configure(relief=SUNKEN)
placeships()
我是否正确使用了
global
,如果是,为什么会出现错误?您没有使用global
正确。必须在函数内将它们声明为全局:
def placeships():
global A1, A2, ...
您没有使用
global
correct。必须在函数内将它们声明为全局:
def placeships():
global A1, A2, ...
global
将本地名称的范围更改为全局名称。因此,它必须进入名称被分配到的范围,而不是全局范围本身
def placeships():
global A1, A2, A3, B1, B2, B3, C1, C2, C3
A1 = Button(window, width=5, height=2, text="A1", command=clickA1).place(x=0, y=0)
A2 = Button(window, width=5, height=2, text="A2", command=clickA2).place(x=50, y=0)
A3 = Button(window, width=5, height=2, text="A3", command=clickA3).place(x=100, y=0)
B1 = Button(window, width=5, height=2, text="B1", command=clickB1).place(x=0, y=50)
B2 = Button(window, width=5, height=2, text="B2", command=clickB2).place(x=50, y=50)
B3 = Button(window, width=5, height=2, text="B3", command=clickB3).place(x=100, y=50)
C1 = Button(window, width=5, height=2, text="C1", command=clickC1).place(x=0, y=100)
C2 = Button(window, width=5, height=2, text="C2", command=clickC2).place(x=50, y=100)
C3 = Button(window, width=5, height=2, text="C3", command=clickC3).place(x=100, y=100)
global
将本地名称的范围更改为全局名称。因此,它必须进入名称被分配到的范围,而不是全局范围本身
def placeships():
global A1, A2, A3, B1, B2, B3, C1, C2, C3
A1 = Button(window, width=5, height=2, text="A1", command=clickA1).place(x=0, y=0)
A2 = Button(window, width=5, height=2, text="A2", command=clickA2).place(x=50, y=0)
A3 = Button(window, width=5, height=2, text="A3", command=clickA3).place(x=100, y=0)
B1 = Button(window, width=5, height=2, text="B1", command=clickB1).place(x=0, y=50)
B2 = Button(window, width=5, height=2, text="B2", command=clickB2).place(x=50, y=50)
B3 = Button(window, width=5, height=2, text="B3", command=clickB3).place(x=100, y=50)
C1 = Button(window, width=5, height=2, text="C1", command=clickC1).place(x=0, y=100)
C2 = Button(window, width=5, height=2, text="C2", command=clickC2).place(x=50, y=100)
C3 = Button(window, width=5, height=2, text="C3", command=clickC3).place(x=100, y=100)