NameError: name 'Frame' is not defined (Python)

余生长醉 提交于 2020-01-04 05:50:10

问题


I have constructed a messaging application, but it seems to have incorrect syntax:

 from tkinter import messagebox


from AESEncDec import *
from MD5Hashing import *
from RSAEncDec import *

color = 'lightblue' #color our background


class Application(Frame):

    def __init__(self, root=None):

        Frame.__init__(self, root)
        self.frame_width = 700
        self.frame_height = 400

        # Set configuration our frame
        self.config(width = self.frame_width, height = self.frame_height, bg = color) 
        self.pack()

        # Create textBox for input data
        self.textbox_one = Text()
        self.textbox_one.place(x = 30, y = 170, height = 200, width = 300 )

        # Create textBox for result
        self.textbox_two = Text()
        self.textbox_two.place(x = 370, y = 170, height = 200, width = 300 )

        label_input_text = Label( text = "Input text: ", bg = color)
        label_input_text.place(x = 30, y = 155, height = 10, width = 70 )

During execution I get the following error:

Traceback (most recent call last): File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 11, in class Application(Frame): NameError: name 'Frame' is not defined

What might be the problem?


回答1:


Frame is a class from the tkinter module.

To fix:

from tkinter import Frame

See an example in the official documentation: https://docs.python.org/3.7/library/tkinter.html#a-simple-hello-world-program

You also need to import Text and Label:

from tkinter import Frame
from tkinter import Text
from tkinter import Label

Or:

from tkinter import *

Here is how you can fix your code (I removed the unused imports):

import tkinter

color = 'lightblue'  # color our background


class Application(tkinter.Frame):
    def __init__(self, root=None):
        super(Application, self).__init__(root)
        self.frame_width = 700
        self.frame_height = 400

        # Set configuration our frame
        self.config(width=self.frame_width, height=self.frame_height, bg=color)
        self.pack()

        # Create textBox for input data
        self.textbox_one = tkinter.Text()
        self.textbox_one.place(x=30, y=170, height=200, width=300)

        # Create textBox for result
        self.textbox_two = tkinter.Text()
        self.textbox_two.place(x=370, y=170, height=200, width=300)

        label_input_text = tkinter.Label(text="Input text: ", bg=color)
        label_input_text.place(x=30, y=155, height=10, width=70)


root = tkinter.Tk()
app = Application(root)
app.mainloop()


来源:https://stackoverflow.com/questions/46252200/nameerror-name-frame-is-not-defined-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!