2017-09-16 65 views
0

我已經建立了一個消息應用程序,但它似乎有不正確的語法:NameError:名字「框架」沒有定義(Python)的

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) 

在執行過程中,我得到以下錯誤:

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

可能是什麼問題?

回答

1

Frame是來自tkinter模塊的一類。

要解決:

from tkinter import Frame 

看到官方文檔中的例子: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 * 

這裏是你如何修復你的代碼(我刪除了未使用的進口):

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() 
相關問題