我对python相对较新,作为一个小项目,我一直在尝试使用tkinter将基本gui应用于我的caesar密码程序。我有点理解什么是错的,但不知道如何解决它。任何帮助或指导将不胜感激。
from tkinter import *
from logos import *
class MainMenu():
def __init__(self,master):
self.master = master
self.master.geometry("500x500")
self.master.title("L337 H4X0R")
self.master.configure(bg = "#303030 ")
self.button = Button(self.master, text = "Encrypt", fg = "green", bg = 'black', command = self.goToEncrypt).place(x=100, y=150)
self.button = Button(self.master, text = "Decrypt", fg = "green", bg = "black", command = self.goToDecrypt).place(x=250, y=150)
self.button = Button(self.master, text = "Hack", fg = "green", bg = "black", command = self.goToHacks).place(x=400, y=150)
def goToEncrypt(self):
self.master.withdraw()
root2 = Toplevel(self.master)
GUII = Encipher(root2)
def goToDecrypt(self):
#root3 = Toplevel(self.master)
#GUII = Decipher(root3)
pass
def goToHacks(self):
#root4 = Toplevel(self.master)
#GUIIkiv = HAXOR(root4)
pass
class Encipher():
def __init__(self,master):
self.key = IntVar()
self.message = StringVar()
self.master = master
self.master.geometry("500x500")
self.master.title("L337 H4X0R")
self.master.configure(bg = "#303030 ")
self.label1 = Label(self.master, text="Key",bg = "black", fg = "green").place(x=20, y = 40)
self.label2 = Label(self.master, text = "Input",bg = "black", fg = "green").place(x=20, y =120)
self.label3 = Label(self.master, text = "Output", bg = "black", fg ="green").place(x=300, y= 120)
self.spinbox= Spinbox(self.master,from_=1, to=61, state = NORMAL, textvariable = self.key, bg ="black", fg="green").place(x=100, y=40)
self.text = Text(self.master, bg = "white", wrap = WORD, height = 15, width = 20).place(x= 20, y=150)
self.text2 = Text(self.master, bg = "white", wrap = WORD, height = 15, width = 20, state = DISABLED).place(x= 300, y=150)
self.button = Button(self.master, text = "Enter", bg = "black", fg = "green", command = self.enter).place(x = 20, y=400)
def enter(self):
self.message = self.text.get('0.0', END+"-1c")
self.text2(self.master, state=NORMAL)
self.text2.insert(self.master,'1.0', text=encrypt(self.message, self.key))
self.text2(self.master, state=DISABLED)
def main():
root = Tk()
GUI = MainMenu(root)
root.mainloop()
if __name__ == "__main__":
main()
当我按下回车按钮时,它会显示以下信息:
C:\Users\Karl\Documents\Python>app.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Programming\Python\lib\tkinter\__init__.py", line 1482, in __call__
return self.func(*args)
File "C:\Users\Karl\Documents\Python\App.py", line 56, in enter
self.message = self.text.get('0.0', END+"-1c")
AttributeError: 'NoneType' object has no attribute 'get'
答案 0 :(得分:4)
请注意,您这样做:
self.text = Text(self.master, bg = "white", wrap = WORD, height = 15, width = 20).place(x= 20, y=150)
所以在self.text
中,您不是存储Text
对象,而是存储place
方法的结果(None
)。
你应该这样做:
self.text = Text(self.master, bg = "white", wrap = WORD, height = 15, width = 20)
self.text.place(x= 20, y=150)
对于您在同一行中创建和调用place()
的所有其他地方也是如此