我是Python的初学者,在友好的Stackoverflow开发人员的帮助下,我成功地构建了下面的代码。
这是一个随机会议主机选择器。你基本上选择一个论坛,并选择一种类型的工程师,它将随机选择一个工程师主持一个会议。
问题:我需要帮助处理电子邮件部分。你如何发送电子邮件时,该人的名字被选中?它需要能够用下拉列表中的电子邮件更新toaddr。邮件主体还必须说:“嗨,你是下一届论坛的主持人,而现在,它说”嗨。!label3,你是下一个论坛的会议主席“。
from tkinter import *
import random
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
root = Tk()
root.title('Random User Entry')
root.geometry("550x600")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(cursor="arrow")
root.configure(highlightbackground="white")
root.configure(highlightcolor="black")
# f = forum, e = engineers
f = IntVar()
e = IntVar()
# Forums
networkcore = ['Tom Reeves', 'Joe Soap', 'John Smith', 'David Jones','Michael Johnson','Chris Lee']
security = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez','Justin Bright', 'Ali Baba']
unifiedcomms = ['Mary White', 'John Smith','Mike Brown','Mark Williams','Paul Rodriguez','Daniel Garcia']
designreview = ["Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves", "Gaye Mccune", "Maricela Chance", "Bret Hazelip"]
automation = ["Renna Geeter", "Ken Stotz", "Nenita Penaflor", "Delena Lumpkins", "Jacqui Noles", "Chau Wardla"]
cloud = ["Nina Perea", "Lila Frederickson", "Brooks Baskette", "Esperanza Slavin","Elisa Duplantis"]
# un = username | eng = engineer
junior_eng_un = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez', 'Justin Bright', 'Ali Baba','Tom Reeves',
'Joe Soap', 'John Smith', 'Mary White', 'John Smith','Mike Brown','Mark, Williams','Paul Rodriguez',
'Daniel Garcia', "Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves",
"Gaye Mccune","Maricela Chance", "Bret Hazelip"]
senior_eng_un = ['David Jones','Michael Johnson','Chris Lee','Mary White', 'John Smith','Mike Brown','Mark Williams',
'Paul Rodriguez','Daniel Garcia', "Nina Perea", "Lila Frederickson" , "Brooks Baskette",
"Esperanza Slavin" ,"Elisa Duplantis"]
def tick():
datenow = datetime.datetime.now()
time_string = datenow.strftime("%d-%m-%Y %H:%M:%S:%p")
clock.config(text=time_string)
clock.after(200, tick)
clock = Label(root, font=("times", 12, "bold"), fg="black", bg="lightgrey")
clock.grid(row=9, column=1, pady=3, padx=20, sticky=NW)
tick()
def update_engineer_list():
global engineers
forum = f.get()
engineer_group = e.get()
if forum and engineer_group:
# both forum and engineer group are selected
forum_engineers = networkcore if forum == 1 else security if forum == 2 else unifiedcomms if forum == 3 \
else designreview if forum == 4 else automation if forum == 5 else cloud
# get available engineers for the selected forum
if engineer_group == 1: # Junior
engineers = [engineer for engineer in junior_eng_un if engineer in forum_engineers]
elif engineer_group == 2: # Senior
engineers = [engineer for engineer in senior_eng_un if engineer in forum_engineers]
elif engineer_group == 3: # all
engineers = forum_engineers
# update engineer list
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
engineer_list.insert(END, "\n".join(sorted(engineers)))
engineer_list.config(state=DISABLED)
# clear host engineer
host_engineer["text"] = "Busy Randomising!!"
def choose_host():
# extract the engineer list from the engineer selection box
engineers = engineer_list.get(1.0, "end-1c").split("\n")
# then select one of them randomly
host_engineer["text"] = random.choice(engineers)
def clear_fields():
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
host_engineer["text"] = ""
return()
def list_email_addr():
email_label = Label(root, text=clicked.get(), font="Helvetica 10 bold",fg="blue")
email_label.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
email_options = ['Click Select Email Address:', "Maria.Lopez@gmail.com",'Raven.Kyle@gmail.com',
'Billy.Joel@gmail.com','James.Gonzalez@gmail.com' ]
clicked = StringVar()
clicked.set(email_options[0])
email_label_drop = OptionMenu(root, clicked, *email_options)
email_label_drop.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
def email_reminder():
# Need to actual from and to address
fromaddr = "abc@abc.com"
toaddr = "def@def.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Meeting Host Reminder "
body = "Hi " + str(host_engineer) + ", You are the meeting chair for the next " + str(update_engineer_list())
msg.attach(MIMEText(body, 'plain'))
# Need to actual domain
server = smtplib.SMTP('smtpmail.domain.com', 25)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
return ()
topLabel = Label(root, text="Random Meeting Host Selector", font="Helvetica 20")
topLabel.grid(row=0, columnspan=2, pady=5, padx=20)
fb2 = Radiobutton(root, text="Security Forum", font="Helvetica 12",variable=f, value=2,command=update_engineer_list)
fb2.grid(row=1, column=0, padx=10, sticky=W)
fb1 = Radiobutton(root, text="Networking Forum", font="Helvetica 12",variable=f, value=1,command=update_engineer_list)
fb1.grid(row=2, column=0, padx=10, sticky=W)
fb3 = Radiobutton(root, text="Unified Comms Forum", font="Helvetica 12",variable=f, value=3,command=update_engineer_list)
fb3.grid(row=3, column=0, padx=10, sticky=W)
fb4 = Radiobutton(root, text="Design Review Forum", font="Helvetica 12",variable=f, value=4,command=update_engineer_list)
fb4.grid(row=4, column=0, padx=10, sticky=W)
fb5 = Radiobutton(root, text="Automation Forum", font="Helvetica 12",variable=f, value=5, command=update_engineer_list)
fb5.grid(row=5, column=0, padx=10, sticky=W)
fb6 = Radiobutton(root, text="Cloud Forum", font="Helvetica 12", variable=f,value=6, command=update_engineer_list)
fb6.grid(row=6, column=0, padx=10, sticky=W)
eb1 = Radiobutton(root, text="Junior Engineers", font="Helvetica 12",variable=e, value=1, command=update_engineer_list)
eb1.grid(row=1, column=1, padx=10, sticky=W)
eb2 = Radiobutton(root, text="Senior Engineers", font="Helvetica 12",variable=e, value=2, command=update_engineer_list)
eb2.grid(row=2, column=1, padx=10, sticky=W)
eb3 = Radiobutton(root, text="All Engineers", font="Helvetica 12",variable=e, value=3, command=update_engineer_list)
eb3.grid(row=3, column=1, padx=10, sticky=W)
hostButton = Button(root, text="Select Next Meeting Host!", font="Helvetica 10 bold", fg="blue", command=choose_host)
hostButton.grid(row=7, column=0, padx=40, sticky=NW)
# engineer list for selection
engineer_list = Text(root, width=30, height=20, bg="lightblue",font="Helvetica 10 bold", relief=SUNKEN, state=DISABLED)
engineer_list.grid(row=7, column=1, padx=10, pady=1, sticky=E)
# host engineer chosen
host_engineer = Label(root, width=20, height=2, bg="lightgreen", font="Helvetica 14 bold", bd=2, relief=SUNKEN)
host_engineer.grid(row=7, column=0, padx=10, pady=33, sticky=N)
emailButton = Button(root, text="Click to Send Email Reminder", font="Helvetica 10 bold", command=email_reminder)
emailButton.grid(row=5, column=1, padx=14, pady=3, sticky=NW)
clearButton = Button(root, text="Clear Selections!", font="Helvetica 10", fg="red", command=clear_fields)
clearButton.grid(row=4, column=1, padx=14, pady=2, sticky=NW)
root.mainloop()发布于 2020-08-10 21:59:39
我试着尽量少修改你的程序。要回答有关主机名称标签字段的问题的第一部分,tkinter使用字典结构来存储小部件的属性。因此,您需要使用host_engineer' text‘检索标签小部件上的文本。
既然您说您是Python新手,我建议您阅读有关列表和字典的内容。字典是非常有用的,但是当你循环它们的时候要小心,订单不能保证!
出于这个原因,我添加了一个“论坛名称”列表,作为每个论坛中用户字典的密钥。这就引出了你问题的第二部分。由于列表的顺序是固定的,它允许我们在创建用户界面上的单选按钮时使用列表,从而可以使用所选单选按钮的值作为索引来查找所选的论坛名称。然后,可以将名称用作forum_users字典中的键。
工程组也可以进行同样的更改,但正如我所说的,我试图尽可能多地保留您的代码。
from tkinter import *
import random
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
root = Tk()
root.title('Random User Entry')
root.geometry("550x600")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(cursor="arrow")
root.configure(highlightbackground="white")
root.configure(highlightcolor="black")
# f = forum, e = engineers
f = IntVar()
e = IntVar()
# list of forum names in the order to be displayed
forum_names = ["Security Forum","Networking Forum","Unified Comms Forum","Design Review Forum","Automation Forum","Cloud Forum"]
# Forums
forum_users = {}
forum_users["Networking Forum"] = ['Tom Reeves', 'Joe Soap', 'John Smith', 'David Jones','Michael Johnson','Chris Lee']
forum_users["Security Forum"] = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez','Justin Bright', 'Ali Baba']
forum_users["Unified Comms Forum"] = ['Mary White', 'John Smith','Mike Brown','Mark Williams','Paul Rodriguez','Daniel Garcia']
forum_users["Design Review Forum"] = ["Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves", "Gaye Mccune", "Maricela Chance", "Bret Hazelip"]
forum_users["Automation Forum"] = ["Renna Geeter", "Ken Stotz", "Nenita Penaflor", "Delena Lumpkins", "Jacqui Noles", "Chau Wardla"]
forum_users["Cloud Forum"] = ["Nina Perea", "Lila Frederickson", "Brooks Baskette", "Esperanza Slavin","Elisa Duplantis"]
# un = username | eng = engineer
junior_eng_un = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez', 'Justin Bright', 'Ali Baba','Tom Reeves',
'Joe Soap', 'John Smith', 'Mary White', 'John Smith','Mike Brown','Mark, Williams','Paul Rodriguez',
'Daniel Garcia', "Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves",
"Gaye Mccune","Maricela Chance", "Bret Hazelip"]
senior_eng_un = ['David Jones','Michael Johnson','Chris Lee','Mary White', 'John Smith','Mike Brown','Mark Williams',
'Paul Rodriguez','Daniel Garcia', "Nina Perea", "Lila Frederickson" , "Brooks Baskette",
"Esperanza Slavin" ,"Elisa Duplantis"]
def tick():
datenow = datetime.datetime.now()
time_string = datenow.strftime("%d-%m-%Y %H:%M:%S:%p")
clock.config(text=time_string)
clock.after(200, tick)
clock = Label(root, font=("times", 12, "bold"), fg="black", bg="lightgrey")
clock.grid(row=9, column=1, pady=3, padx=20, sticky=NW)
tick()
def update_engineer_list():
forum_row = f.get()
engineer_group = e.get()
# both forum and engineer group are selected
if forum and engineer_group:
#get forum users
forum_engineers = forum_users[forum_names[forum_row-1]]
# get available engineers for the selected forum
if engineer_group == 1: # Junior
engineers = [engineer for engineer in junior_eng_un if engineer in forum_engineers]
elif engineer_group == 2: # Senior
engineers = [engineer for engineer in senior_eng_un if engineer in forum_engineers]
elif engineer_group == 3: # all
engineers = forum_engineers
# update engineer list
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
engineer_list.insert(END, "\n".join(sorted(engineers)))
engineer_list.config(state=DISABLED)
# clear host engineer
host_engineer["text"] = "Busy Randomising!!"
def choose_host():
# extract the engineer list from the engineer selection box
engineers = engineer_list.get(1.0, "end-1c").split("\n")
# then select one of them randomly
host_engineer["text"] = random.choice(engineers)
def clear_fields():
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
host_engineer["text"] = ""
return()
def list_email_addr():
email_label = Label(root, text=clicked.get(), font="Helvetica 10 bold",fg="blue")
email_label.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
email_options = ['Click Select Email Address:', "Maria.Lopez@gmail.com",'Raven.Kyle@gmail.com',
'Billy.Joel@gmail.com','James.Gonzalez@gmail.com' ]
clicked = StringVar()
clicked.set(email_options[0])
email_label_drop = OptionMenu(root, clicked, *email_options)
email_label_drop.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
def email_reminder():
# Need to actual from and to address
fromaddr = "abc@abc.com"
toaddr = "def@def.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Meeting Host Reminder "
body = "Hi " + str(host_engineer['text']) + ", You are the meeting chair for the next " + forum_names[f.get()-1] + " meeting."
msg.attach(MIMEText(body, 'plain'))
# Need to actual domain
#server = smtplib.SMTP('smtpmail.domain.com', 25)
text = msg.as_string()
#server.sendmail(fromaddr, toaddr, text)
#server.quit()
print(text)
return ()
topLabel = Label(root, text="Random Meeting Host Selector", font="Helvetica 20")
topLabel.grid(row=0, columnspan=2, pady=5, padx=20)
fblist = []
for row, forum in enumerate(forum_names):
fbtemp = Radiobutton(root, text=forum, font="Helvetica 12",variable=f, value=row+1,command=update_engineer_list)
fbtemp.grid(row=row+1, column=0, padx=10, sticky=W)
fblist.append(fbtemp)
eb1 = Radiobutton(root, text="Junior Engineers", font="Helvetica 12",variable=e, value=1, command=update_engineer_list)
eb1.grid(row=1, column=1, padx=10, sticky=W)
eb2 = Radiobutton(root, text="Senior Engineers", font="Helvetica 12",variable=e, value=2, command=update_engineer_list)
eb2.grid(row=2, column=1, padx=10, sticky=W)
eb3 = Radiobutton(root, text="All Engineers", font="Helvetica 12",variable=e, value=3, command=update_engineer_list)
eb3.grid(row=3, column=1, padx=10, sticky=W)
hostButton = Button(root, text="Select Next Meeting Host!", font="Helvetica 10 bold", fg="blue", command=choose_host)
hostButton.grid(row=7, column=0, padx=40, sticky=NW)
# engineer list for selection
engineer_list = Text(root, width=30, height=20, bg="lightblue",font="Helvetica 10 bold", relief=SUNKEN, state=DISABLED)
engineer_list.grid(row=7, column=1, padx=10, pady=1, sticky=E)
# host engineer chosen
host_engineer = Label(root, width=20, height=2, bg="lightgreen", font="Helvetica 14 bold", bd=2, relief=SUNKEN)
host_engineer.grid(row=7, column=0, padx=10, pady=33, sticky=N)
emailButton = Button(root, text="Click to Send Email Reminder", font="Helvetica 10 bold", command=email_reminder)
emailButton.grid(row=5, column=1, padx=14, pady=3, sticky=NW)
clearButton = Button(root, text="Clear Selections!", font="Helvetica 10", fg="red", command=clear_fields)
clearButton.grid(row=4, column=1, padx=14, pady=2, sticky=NW)
root.mainloop()在电子邮件地址方面,您可以创建一个字典,它使用用户名作为键,并将电子邮件地址存储为值。请记住,字典键必须是唯一的。
实际上,我建议您创建一个小类对象"user“。然后,用户名、电子邮件地址、低年级或高年级、他们所属的论坛等都可以是类的属性。与当前代码相比,这将是一个很大的变化,但从长远来看,它将更容易、更清晰。如果您对它的外观感兴趣,请参见以下内容:
from tkinter import *
import random
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class User(object):
"""docstring for User"""
def __init__(self, name, email, level, forums):
super(User, self).__init__()
self.name = name
self.email = email
self.level = level
self.forums = forums
def __str__(self):
return self.name
# list of forum names in the order to be displayed
forum_names = ["Security Forum","Networking Forum","Unified Comms Forum","Design Review Forum","Automation Forum","Cloud Forum"]
group_names = ["Junior Engineers","Senior Engineers","All Engineers"]
#NOTE: A person must be junior OR senior!
Users = []
#Juniors
Users.append(User('Raven Kyle','rkyle@gmail.com','Junior',["Security Forum",]))
Users.append(User('Billy Joel','bjoel@gmail.com','Junior',["Security Forum",]))
Users.append(User('James Gonzalez','jgon@gmail.com','Junior',["Security Forum",]))
Users.append(User('Maria Lopez','mlopes@gmail.com','Junior',["Security Forum","Automation Forum",]))
Users.append(User('Justin Bright','jbright@gmail.com','Junior',["Security Forum",]))
Users.append(User('Ali Baba','ababa@gmail.com','Junior',["Security Forum",]))
Users.append(User('Tom Reeves','treave@gmail.com','Junior',["Networking Forum",]))
Users.append(User('Joe Soap','jsoep@gmail.com','Junior',["Networking Forum",]))
Users.append(User('John Smith 1','jsmith1@gmail.com','Junior',["Networking Forum",]))
Users.append(User('John Smith 2','jsmith2@gmail.com','Junior',["Unified Comms Forum",]))
Users.append(User('Mike Brown','mbrown@gmail.com','Junior',["Unified Comms Forum","Automation Forum",]))
Users.append(User('Mark, Williams','mwill@gmail.com','Junior',["Unified Comms Forum",]))
Users.append(User("Dave Brazel",'dbra@gmail.com','Junior',["Design Review Forum",]))
Users.append(User("Gwendolyn Vogue",'gvogue@gmail.com','Junior',["Design Review Forum",]))
Users.append(User("Nikole Eaves",'neaves@gmail.com','Junior',["Design Review Forum",]))
Users.append(User("Gaye Mccune",'gmcc@gmail.com','Junior',["Design Review Forum",]))
Users.append(User("Maricela Chance",'mchance@gmail.com','Junior',["Design Review Forum",]))
Users.append(User("Bret Hazelip",'bhaze@gmail.com','Junior',["Design Review Forum","Cloud Forum",]))
#Seniors
Users.append(User('David Jones','dj@gmail.com','Senior',["Networking Forum",]))
Users.append(User('Michael Johnson','mjohnson@gmail.com','Senior',["Networking Forum",]))
Users.append(User('Chris Lee','clee@gmail.com','Senior',["Networking Forum",]))
Users.append(User('Mary White','mwhite@gmail.com','Senior',["Unified Comms Forum",]))
Users.append(User('John Smith 3','js3@gmail.com','Senior',["Unified Comms Forum",]))
Users.append(User('Mike Brown','mbrown@gmail.com','Senior',["Unified Comms Forum",]))
Users.append(User('Paul Rodriguez','prod@gmail.com','Senior',["Unified Comms Forum","Automation Forum",]))
Users.append(User('Daniel Garcia','dgarcia@gmail.com','Senior',["Unified Comms Forum",]))
Users.append(User("Nina Perea",'ninap@gmail.com','Senior',["Design Review Forum","Cloud Forum","Automation Forum",]))
Users.append(User("Lila Frederickson",'lilaf@gmail.com','Senior',["Cloud Forum",]))
Users.append(User("Brooks Baskette",'bbas@gmail.com','Senior',["Cloud Forum",]))
Users.append(User("Esperanza Slavin",'eslavin@gmail.com','Senior',["Cloud Forum",]))
Users.append(User("Elisa Duplantis",'edup@gmail.com','Senior',["Cloud Forum","Automation Forum",]))
#return all users that match the forum and level
def getForumUsers(forum, level):
forum_users = []
for user in Users:
if forum in user.forums:
if level.startswith('All') or level.startswith(user.level):
forum_users.append(user)
return forum_users
#find user object with given name
def findUser(name):
for user in Users:
if user.name == name:
return user
#get the selected forum and level
def getForumAndLevel():
f = selected_forum.get()
g = selected_group.get()
if f > 0 and g > 0:
return forum_names[f-1], group_names[g-1]
return None, None
def update_engineer_list():
forum, level = getForumAndLevel()
#check if both forum and group is selected
if forum and level:
# update engineer list
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
engineer_list.insert(END, "\n".join(sorted(str(usr) for usr in getForumUsers(forum, level))))
engineer_list.config(state=DISABLED)
# clear host engineer
host_engineer["text"] = "Busy Randomising!!"
def choose_host():
# extract the engineer list from the engineer selection box
engineers = engineer_list.get(1.0, "end-1c").split("\n")
# then select one of them randomly
host_engineer["text"] = random.choice(engineers)
def clear_fields():
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
host_engineer["text"] = ""
return()
def email_reminder():
#get the host and forum details
host_user = findUser(host_engineer['text'])
forum, level = getForumAndLevel()
if forum and level and host_user:
# Need to actual from and to address
fromaddr = "meetinggenerator@abc.com"
toaddr = host_user.email
ccaddr = ", ".join(usr.email for usr in getForumUsers(forum, level) if usr != host_user)
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Cc'] = ccaddr
msg['Subject'] = "Meeting Host Reminder "
body = "Hi " + str(host_user) + ", You are the meeting chair for the next " + forum + " meeting."
msg.attach(MIMEText(body, 'plain'))
# Need to actual domain
#server = smtplib.SMTP('smtpmail.domain.com', 25)
text = msg.as_string()
#server.sendmail(fromaddr, toaddr, text)
#server.quit()
print(text)
clear_fields()
root = Tk()
root.title('Random User Entry')
root.geometry("550x600")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(cursor="arrow")
root.configure(highlightbackground="white")
root.configure(highlightcolor="black")
topLabel = Label(root, text="Random Meeting Host Selector", font="Helvetica 20")
topLabel.grid(row=0, columnspan=2, pady=5, padx=20)
#tkinter requires that we keep a referance to all the widgets
widgetlist = []
selected_forum = IntVar()
selected_group = IntVar()
for count, forum in enumerate(forum_names):
fb_temp = Radiobutton(root, text=forum, font="Helvetica 12",variable=selected_forum, value=count+1,command=update_engineer_list)
fb_temp.grid(row=count+1, column=0, padx=10, sticky=W)
widgetlist.append(fb_temp)
for count, group in enumerate(group_names):
eb_temp = Radiobutton(root, text=group, font="Helvetica 12",variable=selected_group, value=count+1, command=update_engineer_list)
eb_temp.grid(row=count+1, column=1, padx=10, sticky=W)
widgetlist.append(eb_temp)
hostButton = Button(root, text="Select Next Meeting Host!", font="Helvetica 10 bold", fg="blue", command=choose_host)
hostButton.grid(row=7, column=0, padx=40, sticky=NW)
# engineer list for selection
engineer_list = Text(root, width=30, height=20, bg="lightblue",font="Helvetica 10 bold", relief=SUNKEN, state=DISABLED)
engineer_list.grid(row=7, column=1, padx=10, pady=1, sticky=E)
# host engineer chosen
host_engineer = Label(root, width=20, height=2, bg="lightgreen", font="Helvetica 14 bold", bd=2, relief=SUNKEN)
host_engineer.grid(row=7, column=0, padx=10, pady=33, sticky=N)
emailButton = Button(root, text="Click to Send Email Reminder", font="Helvetica 10 bold", command=email_reminder)
emailButton.grid(row=5, column=1, padx=14, pady=3, sticky=NW)
clearButton = Button(root, text="Clear Selections!", font="Helvetica 10", fg="red", command=clear_fields)
clearButton.grid(row=4, column=1, padx=14, pady=2, sticky=NW)
root.mainloop()发布于 2020-08-10 21:14:11
对于电子邮件,您可以执行以下操作:
。
这是更新的代码。请注意,我根据这些名字创建了一个假电子邮件列表。您需要手动创建此列表。
from tkinter import *
import random
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
root = Tk()
root.title('Random User Entry')
root.geometry("550x600")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(cursor="arrow")
root.configure(highlightbackground="white")
root.configure(highlightcolor="black")
# f = forum, e = engineers
f = IntVar()
e = IntVar()
# Forums
networkcore = ['Tom Reeves', 'Joe Soap', 'John Smith', 'David Jones','Michael Johnson','Chris Lee']
security = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez','Justin Bright', 'Ali Baba']
unifiedcomms = ['Mary White', 'John Smith','Mike Brown','Mark Williams','Paul Rodriguez','Daniel Garcia']
designreview = ["Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves", "Gaye Mccune", "Maricela Chance", "Bret Hazelip"]
automation = ["Renna Geeter", "Ken Stotz", "Nenita Penaflor", "Delena Lumpkins", "Jacqui Noles", "Chau Wardla"]
cloud = ["Nina Perea", "Lila Frederickson", "Brooks Baskette", "Esperanza Slavin","Elisa Duplantis"]
# un = username | eng = engineer
junior_eng_un = ['Raven Kyle', 'Billy Joel','James Gonzalez','Maria Lopez', 'Justin Bright', 'Ali Baba','Tom Reeves',
'Joe Soap', 'John Smith', 'Mary White', 'John Smith','Mike Brown','Mark, Williams','Paul Rodriguez',
'Daniel Garcia', "Dave Brazel", "Gwendolyn Vogue", "Nikole Eaves",
"Gaye Mccune","Maricela Chance", "Bret Hazelip"]
senior_eng_un = ['David Jones','Michael Johnson','Chris Lee','Mary White', 'John Smith','Mike Brown','Mark Williams',
'Paul Rodriguez','Daniel Garcia', "Nina Perea", "Lila Frederickson" , "Brooks Baskette",
"Esperanza Slavin" ,"Elisa Duplantis"]
# generate fake email dictionary, assumes all names are unique
emaillist = {} # will be dictionary of names > name:email
for lst in [networkcore,security,unifiedcomms,designreview,automation,cloud]: # all names
for nm in lst:
emaillist[nm] = '.'.join(nm.split()) + '@gmail.com' # map name to email
host_email = "" # for email reminder
def tick():
datenow = datetime.datetime.now()
time_string = datenow.strftime("%d-%m-%Y %H:%M:%S:%p")
clock.config(text=time_string)
clock.after(200, tick)
clock = Label(root, font=("times", 12, "bold"), fg="black", bg="lightgrey")
clock.grid(row=9, column=1, pady=3, padx=20, sticky=NW)
tick()
def update_engineer_list():
global engineers
forum = f.get()
engineer_group = e.get()
if forum and engineer_group:
# both forum and engineer group are selected
forum_engineers = networkcore if forum == 1 else security if forum == 2 else unifiedcomms if forum == 3 \
else designreview if forum == 4 else automation if forum == 5 else cloud
# get available engineers for the selected forum
if engineer_group == 1: # Junior
engineers = [engineer for engineer in junior_eng_un if engineer in forum_engineers]
elif engineer_group == 2: # Senior
engineers = [engineer for engineer in senior_eng_un if engineer in forum_engineers]
elif engineer_group == 3: # all
engineers = forum_engineers
# update engineer list
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
engineer_list.insert(END, "\n".join(sorted(engineers)))
engineer_list.config(state=DISABLED)
# clear host engineer
host_engineer["text"] = "Busy Randomising!!"
def choose_host():
global host_email
# extract the engineer list from the engineer selection box
engineers = engineer_list.get(1.0, "end-1c").split("\n")
# then select one of them randomly
host_engineer["text"] = random.choice(engineers)
if host_engineer["text"] in emaillist:
host_email = emaillist[host_engineer["text"]] # for email reminder
else:
print("No email for '"+host_engineer["text"]+"'")
def clear_fields():
engineer_list.config(state=NORMAL)
engineer_list.delete(1.0, END)
host_engineer["text"] = ""
return()
def list_email_addr():
email_label = Label(root, text=clicked.get(), font="Helvetica 10 bold",fg="blue")
email_label.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
email_options = ['Click Select Email Address:', "Maria.Lopez@gmail.com",'Raven.Kyle@gmail.com',
'Billy.Joel@gmail.com','James.Gonzalez@gmail.com' ]
clicked = StringVar()
clicked.set(email_options[0])
email_label_drop = OptionMenu(root, clicked, *email_options)
email_label_drop.grid(row=6, column=1, padx=12, pady=3, sticky=NW)
def email_reminder():
# get forum name
flist = ['Networking Forum','Security Forum','Unified Comms Forum','Design Review Forum','Automation Forum','Cloud Forum']
fname = flist[f.get()-1]
# Need to actual from and to address
fromaddr = "abc@abc.com"
toaddr = host_email
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Meeting Host Reminder "
body = "Hi " + str(host_engineer["text"].split()[0]) + ", You are the meeting chair for the next " + fname
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
print('Email msg:\n',text)
# Need to actual domain
server = smtplib.SMTP('smtpmail.domain.com', 25)
server.sendmail(fromaddr, toaddr, text)
server.quit()
return ()
topLabel = Label(root, text="Random Meeting Host Selector", font="Helvetica 20")
topLabel.grid(row=0, columnspan=2, pady=5, padx=20)
fb2 = Radiobutton(root, text="Security Forum", font="Helvetica 12",variable=f, value=2,command=update_engineer_list)
fb2.grid(row=1, column=0, padx=10, sticky=W)
fb1 = Radiobutton(root, text="Networking Forum", font="Helvetica 12",variable=f, value=1,command=update_engineer_list)
fb1.grid(row=2, column=0, padx=10, sticky=W)
fb3 = Radiobutton(root, text="Unified Comms Forum", font="Helvetica 12",variable=f, value=3,command=update_engineer_list)
fb3.grid(row=3, column=0, padx=10, sticky=W)
fb4 = Radiobutton(root, text="Design Review Forum", font="Helvetica 12",variable=f, value=4,command=update_engineer_list)
fb4.grid(row=4, column=0, padx=10, sticky=W)
fb5 = Radiobutton(root, text="Automation Forum", font="Helvetica 12",variable=f, value=5, command=update_engineer_list)
fb5.grid(row=5, column=0, padx=10, sticky=W)
fb6 = Radiobutton(root, text="Cloud Forum", font="Helvetica 12", variable=f,value=6, command=update_engineer_list)
fb6.grid(row=6, column=0, padx=10, sticky=W)
eb1 = Radiobutton(root, text="Junior Engineers", font="Helvetica 12",variable=e, value=1, command=update_engineer_list)
eb1.grid(row=1, column=1, padx=10, sticky=W)
eb2 = Radiobutton(root, text="Senior Engineers", font="Helvetica 12",variable=e, value=2, command=update_engineer_list)
eb2.grid(row=2, column=1, padx=10, sticky=W)
eb3 = Radiobutton(root, text="All Engineers", font="Helvetica 12",variable=e, value=3, command=update_engineer_list)
eb3.grid(row=3, column=1, padx=10, sticky=W)
hostButton = Button(root, text="Select Next Meeting Host!", font="Helvetica 10 bold", fg="blue", command=choose_host)
hostButton.grid(row=7, column=0, padx=40, sticky=NW)
# engineer list for selection
engineer_list = Text(root, width=30, height=20, bg="lightblue",font="Helvetica 10 bold", relief=SUNKEN, state=DISABLED)
engineer_list.grid(row=7, column=1, padx=10, pady=1, sticky=E)
# host engineer chosen
host_engineer = Label(root, width=20, height=2, bg="lightgreen", font="Helvetica 14 bold", bd=2, relief=SUNKEN)
host_engineer.grid(row=7, column=0, padx=10, pady=33, sticky=N)
emailButton = Button(root, text="Click to Send Email Reminder", font="Helvetica 10 bold", command=email_reminder)
emailButton.grid(row=5, column=1, padx=14, pady=3, sticky=NW)
clearButton = Button(root, text="Clear Selections!", font="Helvetica 10", fg="red", command=clear_fields)
clearButton.grid(row=4, column=1, padx=14, pady=2, sticky=NW)
root.mainloop()https://stackoverflow.com/questions/63347216
复制相似问题