带有 .pack() 作为无效语法的 Tkinter Button 小部件

Tkinter Button widget with .pack() as an invalid syntax

我正在 Python 3.5.3 中的 tkinter 中制作一个基本的井字游戏,但我 运行 遇到一个错误,说 .pack() 是一个无效的语法。代码:

from tkinter import *
root = Tk()
turn = X
1 = Button(root, command=Pressed)
1.pack()
def Pressed():
    pass
root.geometry('900x900')
root.mainloop()

谁能给我一个答案。

您为按钮分配的变量名称无效。 尝试重写

1 = Button(root, command=Pressed)
1.pack()

类似于:

button_1 = Button(root, command=Pressed)
button_1.pack()

变量名注意事项:

  • 必须以字母 (a - z, A - B) 或下划线 (_) 开头

  • 其他字符可以是字母、数字或_

  • 区分大小写

  • 可以是任何(合理的)长度

  • 有些保留字不能用作变量名 因为 Python 将它们用于其他用途。