NameError:name' tk'未在对象类中定义

时间:2017-09-16 12:37:12

标签: python tkinter nameerror

我构建了一个应用程序,源代码如下:

from tkinter import Text
from tkinter import Label    

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

但最后一段不能执行:

#create object TK class
the_window = Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()

它给出了NameError:

  

追踪(最近一次通话):     文件" / home / artur / Documents / MScProject / MSc项目/任务#179276 / main_program.py",第169行,in       the_window = Tk(className =" Cryptographic")   NameError:name' Tk'未定义

在这种情况下,我该如何正确定义?

1 个答案:

答案 0 :(得分:1)

您错过了导入声明:from tkinter import Tk

避免冲突的最佳方法是导入整个模块,最后使用别名将其缩短(但不要忘记在您称为tkinter小部件的任何地方添加tk. ):

import tkinter as tk

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

color = 'lightblue' #color our background

class Application(tk.Frame):

    def __init__(self, root=None):

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


#create object TK class
the_window = tk.Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()