NameError:未定义名称“框架"(Python) [英] NameError: name 'Frame' is not defined (Python)

查看:139
本文介绍了NameError:未定义名称“框架"(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个消息传递应用程序,但是它的语法似乎不正确:

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:

回溯(最近通话最近): 在第11行的"/home/artur/Documents/MScProject/MSc Project/Task#179276/main_program.py"文件中 类Application(Frame): NameError:未定义名称框架"

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

可能是什么问题?

推荐答案

要解决:

from tkinter import Frame

请参阅官方文档中的示例: https://docs.python.org/3.7/library/tkinter.html#a-simple-hello-world-program

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

您还需要导入TextLabel:

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

或者:

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()

这篇关于NameError:未定义名称“框架"(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆