Python Tkinter 不工作。名称错误

Python Tkinter not working. Name error

非常简单的代码,但无法在 Python 中使用 tkinder。

此代码已被复制,就像在视频教程中看到的那样,所以我认为它可以是任何配置:

from tkinter import*

root=Tk()

miFrame=Frame(root, width=500, height=400)

miFrame=pack()

Label(miFrame, text="Hola alumnos de Python", fg="red", font=("Comic Sans 
MS", 18)).place(x=100, y=200)

root.mainloop()

错误:

Traceback (most recent call last): File "Prueba2.py", line 7, in miFrame=pack() NameError: name 'pack' is not defined

miFrame=pack()替换为miFrame.pack()

miFrame=pack() 试图将符号 miFrame 分配给对某些已知的引用函数 pack()

在一个案例中, python 解释器状态不知道这样的情况,它抛出了上面提到的异常。

但是,对象 miFrame,是分配给 tkinter.Frame 实例的对象的上一行,有一个 instance-method 存在 - .pack(),可以调用,而不是直接 re-assign miFrame,实例化之后:

miFrame = Frame( root, width  = 500,
                       height = 400
                       )
miFrame.pack() #_____________________ .pack() is a Frame-class instance-method

Label( miFrame, text = "Hola alumnos de Python",
                fg   = "red",
                font = ( "Comic Sans MS", 18 )
                ).place( x = 100,
                         y = 200
                         )