Python 正在尝试创建电子邮件日志系统,但数组保持空白
所以我尝试创建一种电子邮件日志系统,每行都有主题和消息预览,比如gmail 在Python 正在尝试创建电子邮件日志系统,但数组保持空白,python,arrays,tkinter,Python,Arrays,Tkinter,所以我尝试创建一种电子邮件日志系统,每行都有主题和消息预览,比如gmail 在mainWindowsScript.py中,我有一个“编写”按钮,用于将字符串附加到getLogs.py中的数组中,还有一个“日志”按钮,使用操作系统模块运行getLogs.py。在getLogs.py中,我只有一个ListBox。我使用tkinter创建按钮和列表框 当我运行mainWindowsScript.py时,它首先打开在getLogs.py中创建的列表框。但奇怪的是,当我关闭该窗口并单击“撰写”按钮时,我试
mainWindowsScript.py
中,我有一个“编写”按钮,用于将字符串附加到getLogs.py
中的数组中,还有一个“日志”按钮,使用操作系统模块运行getLogs.py
。在getLogs.py
中,我只有一个ListBox
。我使用tkinter创建按钮和列表框
当我运行mainWindowsScript.py
时,它首先打开在getLogs.py
中创建的列表框。但奇怪的是,当我关闭该窗口并单击“撰写”按钮时,我试图附加到的列表仍然是空的,使得列表框
为空
真让人困惑,我需要帮助
这里是mainWindowsScript.py
:
import tkinter as tk
from tkinter import ttk
import os
import getLogs as gl
mainWin = tk.Tk()
canvas = tk.Canvas(mainWin, width = 400, height = 400)
canvas.pack()
frame = tk.Frame(mainWin, bg = "gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
def compose():
gl.tos.append("isybgf")
#os.system("python email_gui.py") #run another python file
def getEmailLogs():
os.system("python getLogs.py")
composeButton = tk.Button(frame, bg = "blue", fg = "white", text = "COMPOSE", command = compose)
composeButton.place(relx = 0.3, rely = 0.2, relwidth = 0.4, relheight = 0.2)
logsButton = tk.Button(frame, bg = "blue", fg = "white", text = "LOGS", command = getEmailLogs)
logsButton.place(relx = 0.3, rely = 0.5, relwidth = 0.4, relheight = 0.2)
mainWin.mainloop()
import tkinter as tk
mainWind = tk.Tk()
canvas = tk.Canvas(mainWind, width = 400, height = 600)
canvas.pack()
frame = tk.Frame(mainWind, bg = "gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
mailLogs = tk.Listbox(frame)
mailLogs.place(relx = 0.1, rely = 0.05, relwidth = 0.8, relheight = 0.9)
tos = []
subjects = []
messages = []
print(tos)
mails = {
"to:": tos,
"subject:": subjects,
"message:": messages
}
for i in range(0, len(tos)):
emailLogTitle = "{}: {} - {}".format(mails["to:"][i], mails["subject:"][i], mails["message:"][i])
mailLogs.insert('end', emailLogTitle)
mainWind.mainloop()
和getLogs.py
:
import tkinter as tk
from tkinter import ttk
import os
import getLogs as gl
mainWin = tk.Tk()
canvas = tk.Canvas(mainWin, width = 400, height = 400)
canvas.pack()
frame = tk.Frame(mainWin, bg = "gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
def compose():
gl.tos.append("isybgf")
#os.system("python email_gui.py") #run another python file
def getEmailLogs():
os.system("python getLogs.py")
composeButton = tk.Button(frame, bg = "blue", fg = "white", text = "COMPOSE", command = compose)
composeButton.place(relx = 0.3, rely = 0.2, relwidth = 0.4, relheight = 0.2)
logsButton = tk.Button(frame, bg = "blue", fg = "white", text = "LOGS", command = getEmailLogs)
logsButton.place(relx = 0.3, rely = 0.5, relwidth = 0.4, relheight = 0.2)
mainWin.mainloop()
import tkinter as tk
mainWind = tk.Tk()
canvas = tk.Canvas(mainWind, width = 400, height = 600)
canvas.pack()
frame = tk.Frame(mainWind, bg = "gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
mailLogs = tk.Listbox(frame)
mailLogs.place(relx = 0.1, rely = 0.05, relwidth = 0.8, relheight = 0.9)
tos = []
subjects = []
messages = []
print(tos)
mails = {
"to:": tos,
"subject:": subjects,
"message:": messages
}
for i in range(0, len(tos)):
emailLogTitle = "{}: {} - {}".format(mails["to:"][i], mails["subject:"][i], mails["message:"][i])
mailLogs.insert('end', emailLogTitle)
mainWind.mainloop()
使用os.system()
运行单独的程序,该程序无法访问当前程序中创建的数据。因为您导入了getLogs
,所以您应该将代码放入函数中,以便在没有os.system()的情况下执行它
getLogs.py
import tkinter as tk
tos = []
subjects = []
messages = []
def log_window():
#mainWind = tk.Tk()
mainWind = tk.Toplevel()
canvas = tk.Canvas(mainWind, width = 400, height = 600)
canvas.pack()
frame = tk.Frame(mainWind, bg = "gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
mailLogs = tk.Listbox(frame)
mailLogs.place(relx = 0.1, rely = 0.05, relwidth = 0.8, relheight = 0.9)
print(tos)
mails = {
"to:": tos,
"subject:": subjects,
"message:": messages
}
for i in range(0, len(tos)):
emailLogTitle = "{}: {} - {}".format(mails["to:"][i], mails["subject:"][i], mails["message:"][i])
mailLogs.insert('end', emailLogTitle)
#mainWind.mainloop()
现在你可以执行了
def getEmailLogs():
gl.log_window()
并且它可以访问tos
因为两个窗口在同一个程序中运行,所以second不需要mainloop()
,它应该由Toplevel()
创建,而不是Tk()
顺便说一句:composer只添加了tos
,所以后来的字典mail
没有主题和消息。您可以创建一个包含邮件的列表,而不是为收件人
,主题
,消息
麦皮
import tkinter as tk
import os
import getLogs as gl
# --- functions ---
def compose():
gl.tos.append("isybgf")
gl.subjects.append("isybgf")
gl.messages.append("isybgf")
# it could be better to keep it in one list ie.
# mails.append( {'to': "isybgf", 'subject': "isybgf", 'message': "isybgf" } )
def getEmailLogs():
gl.log_window()
# --- main ---
mainWin = tk.Tk()
canvas = tk.Canvas(mainWin, width=400, height=400)
canvas.pack()
frame = tk.Frame(mainWin, bg="gray")
frame.place(relx = 0, rely = 0, relwidth = 1, relheight = 1)
composeButton = tk.Button(frame, bg="blue", fg="white", text="COMPOSE", command=compose)
composeButton.place(relx=0.3, rely=0.2, relwidth=0.4, relheight=0.2)
logsButton = tk.Button(frame, bg="blue", fg="white", text="LOGS", command=getEmailLogs)
logsButton.place(relx=0.3, rely=0.5, relwidth=0.4, relheight=0.2)
mainWin.mainloop()
编辑:带有其他更改的版本-减少为重要元素。我将邮件保存在主程序中的一个列表mails
,并作为参数发送到日志
main.py
import tkinter as tk
import getLogs as gl
# --- functions ---
def compose():
mails.append( {'to': "user@domain.com", 'subject': "Read it", 'message': "Hello World!"} )
def get_email_logs():
gl.log_window(mails)
# --- main --- (lower_case_names)
mails = []
main_window = tk.Tk()
compose_button = tk.Button(main_window, text="COMPOSE", command=compose)
compose_button.pack(fill='x')
logs_button = tk.Button(main_window, text="LOGS", command=get_email_logs)
logs_button.pack(fill='x')
close_button = tk.Button(main_window, text="CLOSE", command=main_window.destroy)
close_button.pack(fill='x')
main_window.mainloop()
getLogs.py
import tkinter as tk
def log_window(mails):
window = tk.Toplevel()
mail_logs = tk.Listbox(window)
mail_logs.pack(fill='both', expand=True)
close_button = tk.Button(window, text="CLOSE", command=window.destroy)
close_button.pack(fill='x')
for item in mails:
mail_logs.insert('end', "{}: {} - {}".format( item["to"], item["subject"], item["message"] ))
将代码放入函数中,并在导入后运行函数。使用os.system(“python getLogs.py”)
您运行一个单独的程序,该程序对您在mainWindowsScript
中创建的列表tos
一无所知-它以自己的空列表tos
开始,并且无法访问在mainWindowsScript.py
中创建的数据。顺便说一句:当您导入import getLogs as gl
时,您将自动从中运行代码getLogs.py
所以您可以在开始时看到它。如果您将代码放入函数中,那么它将不会在开始时运行它,当您按下按钮时,您可以将其作为gl.function()
运行-如果您想作为os.system(“python getLogs.py”)运行,则不使用os.system()
然后您必须将tos
中的数据保存到文件中,getLogs.py
必须从文件中读取这些数据。请继续阅读,感谢所有提供解决方案的人!这真的很有帮助。