我正在尝试创建一个简单的Tkinter
脚本,该脚本包含三个变量(名字,姓氏,公司)并生成一些电子邮件可能性。
这是我的代码:
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.firstname = Entry(self, text="First")
self.firstname.grid(row= 0, column = 0, columnspan = 2, sticky = W)
self.firstname.insert(0, "First Name")
self.lastname = Entry(self)
self.lastname.grid(row= 1, column = 0, columnspan = 2, sticky = W)
self.lastname.insert(0, "Last Name")
self.companyname = Entry(self)
self.companyname.grid(row= 2, column = 0, columnspan = 2, sticky = W)
self.companyname.insert(0, "Company Name")
self.button = Button(self, text="Generate!", command=self.gen_email(self.firstname.get(),self.lastname.get(),self.companyname.get()))
self.button.grid()
#where the generated emails will go
self.text = Text(self, width = 35, height=5, wrap =WORD)
self.text.grid(row=4,column=0, columnspan=2, sticky=W)
def gen_email(self, firstname, lastname, companyname):
self.email_list = []
self.email_list.append(firstname + "." + lastname + "@" + companyname + ".com")
self.email_list.append(firstname[0] + lastname + "@" + companyname + ".com")
self.email_list.append(firstname + lastname[0] + "@" + companyname + ".com")
self.email_list.append(firstname + "@" + companyname + ".com")
self.email_list.append(firstname + "_" + lastname +"@" + companyname + ".com")
self.text.insert(0.0, self.email_list)
root = Tk()
root.title("Email Generator")
root.geometry("400x400")
app = Application(root)
root.mainloop()
尝试在屏幕上显示生成的电子邮件时收到错误消息:
Traceback (most recent call last):
File "emailgen.py", line 65, in <module>
app = Application(root)
File "emailgen.py", line 21, in __init__
self.create_widgets()
File "emailgen.py", line 39, in create_widgets
self.button = Button(self, text="Generate!", command=self.gen_email(self.firstname.get(),self.lastname.get(),self.companyname.get()))
File "emailgen.py", line 56, in gen_email
self.text.insert(0.0, self.email_list)
AttributeError: Application instance has no attribute 'text'
我在此处收到错误:self.text.insert(0.0, self.email_list)
,但在程序中定义了self.text
并已添加到网格中。我怎样才能使这个功能起作用,因为我似乎在这里遗漏了一些东西。
答案 0 :(得分:2)
您需要为按钮命令提供lambda:
self.button = Button(self, text="Generate!",
command=lambda :self.gen_email(self.firstname.get(),
self.lastname.get(),
self.companyname.get()))
更好的方法是拥有
gen_email函数中的self.firstname.get(), self.lastname.get(), self.companyname.get()
。这样你就可以消除lambda:
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.firstname = Entry(self, text="First")
self.firstname.grid(row= 0, column = 0, columnspan = 2, sticky = W)
self.firstname.insert(0, "First Name")
self.lastname = Entry(self)
self.lastname.grid(row= 1, column = 0, columnspan = 2, sticky = W)
self.lastname.insert(0, "Last Name")
self.companyname = Entry(self)
self.companyname.grid(row= 2, column = 0, columnspan = 2, sticky = W)
self.companyname.insert(0, "Company Name")
self.button = Button(self, text="Generate!",
command=self.gen_email)
self.button.grid()
#where the generated emails will go
self.text = Text(self, width = 35, height=5, wrap =WORD)
self.text.grid(row=4,column=0, columnspan=2, sticky=W)
def gen_email(self):
firstname = self.firstname.get()
lastname = self.lastname.get()
companyname = self.companyname.get()
self.email_list = []
self.email_list.append(firstname + "." + lastname + "@" + companyname + ".com")
self.email_list.append(firstname[0] + lastname + "@" + companyname + ".com")
self.email_list.append(firstname + lastname[0] + "@" + companyname + ".com")
self.email_list.append(firstname + "@" + companyname + ".com")
self.email_list.append(firstname + "_" + lastname +"@" + companyname + ".com")
self.text.insert(0.0, self.email_list)
root = Tk()
root.title("Email Generator")
root.geometry("400x400")
app = Application(root)
root.mainloop()