将pyqt5转换为exe时出错:转换pyqt5设计器创建的代码后,出现“无法执行脚本”
# -*- coding: utf-8 -*-
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
from imapclient import *
class TheWindow(QMainWindow):
def __init__(self):
super(TheWindow, self).__init__()
self.setObjectName("Main_Window")
self.resize(840, 600)
self.setWindowTitle("Mail Cleaner")
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName("centralwidget")
# FRAME
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(10, 0, 821, 591))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
# #### EMAIL
self.label_email = QtWidgets.QLabel(self.frame)
self.label_email.setGeometry(QtCore.QRect(50, 20, 61, 21))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label_email.setFont(font)
self.label_email.setObjectName("label_email")
self.label_email.setText("E-Mail")
self.lineedit_yourmail = QtWidgets.QLineEdit(self.frame)
self.lineedit_yourmail.setGeometry(QtCore.QRect(50, 40, 271, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(11)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.lineedit_yourmail.setFont(font)
self.lineedit_yourmail.setObjectName("lineedit_yourmail")
# self.lineedit_yourmail.setText("[email protected]")
self.lineedit_yourmail.setPlaceholderText("Enter your email")
# ### PASSWORD
self.label_password = QtWidgets.QLabel(self.frame)
self.label_password.setGeometry(QtCore.QRect(340, 20, 101, 21))
self.label_password.setFont(font)
self.label_password.setObjectName("label_password")
self.label_password.setText("Password")
self.lineedit_password = QtWidgets.QLineEdit(self.frame)
self.lineedit_password.setGeometry(QtCore.QRect(340, 40, 201, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(11)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.lineedit_password.setFont(font)
self.lineedit_password.setObjectName("lineedit_password")
# self.lineedit_password.setText("ahmed01273290218")
self.lineedit_password.setPlaceholderText("Enter your mail password")
self.lineedit_password.setEchoMode(QtWidgets.QLineEdit.Password)
# TEXT BROWSER
self.textBrowser = QtWidgets.QTextBrowser(self.frame)
self.textBrowser.setGeometry(QtCore.QRect(55, 201, 561, 221))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.textBrowser.setFont(font)
self.textBrowser.setObjectName("textBrowser")
self.textBrowser.setHtml("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Times New Roman\'; font-size:11pt; font-weight:600; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>")
# PROGRESS BAR
self.progressbar = QtWidgets.QProgressBar(self.frame)
self.progressbar.setGeometry(QtCore.QRect(60, 490, 561, 23))
self.progressbar.setMinimum(1)
self.progressbar.setProperty("value", 0)
self.progressbar.setObjectName("progressbar")
self.button_login = QtWidgets.QPushButton(self.frame)
self.button_login.setGeometry(QtCore.QRect(550, 40, 61, 31))
self.button_login.setObjectName("button_login")
self.button_login.clicked.connect(self.login)
self.button_login.setText("Login")
self.button_delete = QtWidgets.QPushButton(self.frame)
self.button_delete.setGeometry(QtCore.QRect(640, 430, 141, 81))
font = QtGui.QFont()
font.setPointSize(2)
font.setBold(True)
font.setWeight(75)
self.button_delete.setFont(font)
self.button_delete.setObjectName("button_delete")
self.button_delete.clicked.connect(self.delete_messages)
self.button_delete.setDisabled(True)
self.button_delete.setText("Delete")
self.button_exit = QtWidgets.QPushButton(self.frame)
self.button_exit.setGeometry(QtCore.QRect(290, 540, 101, 31))
self.button_exit.setObjectName("button_exit")
self.button_exit.clicked.connect(self.close_app_exit)
self.button_exit.setText("EXIT")
self.button_addmail = QtWidgets.QPushButton(self.frame)
self.button_addmail.setGeometry(QtCore.QRect(510, 120, 101, 31))
self.button_addmail.setObjectName("button_addmail")
self.button_addmail.clicked.connect(self.add_mail)
self.button_addmail.setDisabled(True)
self.button_addmail.setText("Add mail to list")
self.button_clear = QtWidgets.QPushButton(self.frame)
self.button_clear.setGeometry(QtCore.QRect(290, 440, 101, 31))
self.button_clear.setObjectName("button_clear")
self.button_clear.clicked.connect(self.clear_text_browser)
self.button_clear.setText("Clear List")
self.lineedit_mailer = QtWidgets.QLineEdit(self.frame)
self.lineedit_mailer.setGeometry(QtCore.QRect(50, 120, 441, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.lineedit_mailer.setFont(font)
self.lineedit_mailer.setObjectName("lineedit_mailer")
# self.lineedit_mailer.setText("[email protected]")
self.lineedit_mailer.setPlaceholderText("Enter email you wish delete it's messages")
self.lineedit_mailer.setDisabled(True)
self.label_mailer = QtWidgets.QLabel(self.frame)
self.label_mailer.setGeometry(QtCore.QRect(50, 85, 91, 31))
self.label_mailer.setFont(font)
self.label_mailer.setObjectName("label_mailer")
self.label_mailer.setText("E-mail From")
self.label_emailslistname = QtWidgets.QLabel(self.frame)
self.label_emailslistname.setGeometry(QtCore.QRect(60, 170, 171, 31))
self.label_emailslistname.setFont(font)
self.label_emailslistname.setObjectName("label_emailslistname")
self.label_emailslistname.setText("Emails You will delete")
self.label_progress = QtWidgets.QLabel(self.frame)
self.label_progress.setGeometry(QtCore.QRect(60, 460, 71, 21))
self.label_progress.setFont(font)
self.label_progress.setObjectName("label_progress")
self.label_progress.setText("Progress")
self.label_mailinfo = QtWidgets.QLabel(self.frame)
self.label_mailinfo.setGeometry(QtCore.QRect(640, 200, 141, 221))
font.setFamily("Times New Roman")
self.label_mailinfo.setFont(font)
self.label_mailinfo.setAutoFillBackground(False)
self.label_mailinfo.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_mailinfo.setScaledContents(False)
self.label_mailinfo.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
self.label_mailinfo.setWordWrap(True)
self.label_mailinfo.setObjectName("label_mailinfo")
self.label_mailinfo.setText("information about activate mail")
self.label_emailconfirmed = QtWidgets.QLabel(self.frame)
self.label_emailconfirmed.setGeometry(QtCore.QRect(640, 40, 121, 31))
self.label_emailconfirmed.setFont(font)
self.label_emailconfirmed.setObjectName("label_emailconfirmed")
self.label_emailconfirmed.setText("Please Login")
self.comboBox = QtWidgets.QComboBox(self.frame)
self.comboBox.setGeometry(QtCore.QRect(660, 120, 71, 31))
font = QtGui.QFont()
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("English")
self.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(self)
self.deleted_mails = []
# ============================================= FUNCTIONS =================================================
imaplib._MAXLINE = 1000000
def auto_detect_mail(self):
# auto detect mail type (yahoo, Gmail, hotmail)
if '@yahoo.com' in self.lineedit_yourmail.text():
# print("auto_detect_mail(self): yahoo mail")
return 'yahoo'
elif '@gmail.com' in self.lineedit_yourmail.text():
# print("auto_detect_mail(self): Gmail")
return 'gmail'
elif '@hotmail.com' in self.lineedit_yourmail.text():
# print("auto_detect_mail(self): hotmail")
return "hotmail"
else:
return False
def internet_connection(self):
# check internet connection
url = 'http://www.google.com/'
timeout = 5
try:
requests.get(url, timeout=timeout)
return True
except requests.ConnectionError:
self.label_emailconfirmed.setText("No internet")
return False
def login(self):
try:
self.lineedit_mailer.setDisabled(True)
self.button_addmail.setDisabled(True)
self.button_delete.setDisabled(True)
# login email using address and password
# yahoo mail
if self.internet_connection() and self.auto_detect_mail() == 'yahoo':
self.label_mailinfo.setText("please unprotect your email goto link yahoo imap activate")
self.yahoo_e_mail = self.lineedit_yourmail.text()
self.yahoo_password = self.lineedit_password.text()
self.yahoo_imap_Object = imapclient.IMAPClient('imap.mail.yahoo.com', ssl=True, port="993")
self.yahoo_imap_Object.login(self.yahoo_e_mail, self.yahoo_password)
self.label_emailconfirmed.setText("yahoo connected")
# print("you have internet connection and yahoo register")
self.lineedit_mailer.setDisabled(False)
self.button_addmail.setDisabled(False)
self.button_delete.setDisabled(False)
return True
# Gmail
elif self.internet_connection() and self.auto_detect_mail() == 'gmail':
self.label_mailinfo.setText("please unprotect your email goto link gmail imap activate")
self.gmail_e_mail = self.lineedit_yourmail.text()
self.gmail_password = self.lineedit_password.text()
self.gmail_imap_Object = imapclient.IMAPClient('imap.gmail.com', ssl=True)
self.gmail_imap_Object.login(self.gmail_e_mail, self.gmail_password)
self.label_emailconfirmed.setText("Gmail connected")
# print("you have internet connection and 'gmail' register")
self.lineedit_mailer.setDisabled(False)
self.button_addmail.setDisabled(False)
self.button_delete.setDisabled(False)
return True
# Hotmail
elif self.internet_connection() and self.auto_detect_mail() == 'hotmail':
self.label_mailinfo.setText("please unprotect your email goto link hotmail imap activate")
self.hotmail = self.lineedit_yourmail.text()
self.hotmail_password = self.lineedit_password.text()
Server_name = "outlook.office365.com"
self.hotmail_imap_Object = imapclient.IMAPClient(Server_name, ssl=True, port=993)
self.hotmail_imap_Object.login(self.hotmail, self.hotmail_password)
self.label_emailconfirmed.setText("hotmail connected")
# print("you have internet connection and hotmail register")
self.lineedit_mailer.setDisabled(False)
self.button_addmail.setDisabled(False)
self.button_delete.setDisabled(False)
return True
else:
if self.internet_connection():
# print("Unknown email Or wrong email")
return False
except:
self.label_mailinfo.setText("Wrong mail or password")
self.label_emailconfirmed.setText("Error")
# print("Unknown email")
def add_mail(self): # so complicated
# check if the mail have record messages if it haven't don't add
# take the all messages from sender and store it in dictionary {key(sender): value(count)}.
if self.lineedit_mailer.text() not in self.deleted_mails and "@" in self.lineedit_mailer.text():
self.deleted_mails.append(self.lineedit_mailer.text())
self.lineedit_mailer.clear()
else:
self.label_mailinfo.setText("Repeated Mail or wrong mailer")
return False
try:
# login email using address and password
# yahoo mail
if self.internet_connection():
if self.auto_detect_mail() == 'yahoo':
self.yahoo_imap_Object.select_folder('INBOX', readonly=False)
for link in self.deleted_mails:
search_address = f"FROM {link}"
self.yahoo_UIDs = self.yahoo_imap_Object.search(search_address)
# print(self.yahoo_UIDs)
for i in self.yahoo_UIDs:
self.content = f"FROM: {self.deleted_mails[-1]}, MESSAGES = {len(self.yahoo_UIDs)}"
# print(self.content)
self.textBrowser.append(str(self.content))
# Gmail
elif self.auto_detect_mail() == 'gmail': # CONFIRMED FUNCTION
self.gmail_imap_Object.select_folder('INBOX', readonly=False)
for link in self.deleted_mails:
search = "FROM {}".format(link)
self.gmail_UIDs = self.gmail_imap_Object.search(search)
for i in self.gmail_UIDs:
self.content = f"FROM: {self.deleted_mails[-1]}, MESSAGES = {len(self.gmail_UIDs)}"
self.textBrowser.append(str(self.content))
# Hotmail
elif self.auto_detect_mail() == 'hotmail':
self.hotmail_imap_Object.select_folder('INBOX', readonly=False)
for link in self.deleted_mails:
search = "FROM {}".format(link)
self.hotmail_UIDs = self.hotmail_imap_Object.search(search)
# print(self.hotmail_UIDs)
for i in self.hotmail_UIDs:
self.content = f"FROM: {self.deleted_mails[-1]}, MESSAGES = {len(self.hotmail_UIDs)}"
# print(self.deleted_mails)
self.textBrowser.append(str(self.content))
else:
if self.internet_connection():
self.label_mailinfo.setText("Write Valid Mail")
# print("Unknown email Or wrong email")
except:
# self.label_mailinfo.setText("please select\nyour email type")
self.label_emailconfirmed.setText("choose mail")
# print("Unknown email")
return False
def delete_messages(self): # take mails in the list and delete it if it isn't empty
# connect progressbar to delete messages and delete all messages stored in textBrowser
if len(self.deleted_mails) > 0:
if self.auto_detect_mail() == 'gmail':
for link in self.deleted_mails:
from_search = "FROM {}".format(link)
self.after_gmail_UIDs = self.gmail_imap_Object.search(from_search)
for i in self.after_gmail_UIDs:
self.gmail_imap_Object.delete_messages(i)
self.label_mailinfo.setText("Process Finished")
return True
elif self.auto_detect_mail() == 'yahoo':
for link in self.deleted_mails:
from_search = "FROM {}".format(link)
self.after_yahoo_UIDs = self.yahoo_imap_Object.search(from_search)
for i in self.after_yahoo_UIDs:
self.yahoo_imap_Object.delete_messages(i)
self.label_mailinfo.setText("Process Finished")
return True
elif self.auto_detect_mail() == 'hotmail':
for link in self.deleted_mails:
from_search = "FROM {}".format(link)
self.after_hotmail_UIDs = self.hotmail_imap_Object.search(from_search)
for i in self.after_hotmail_UIDs:
self.hotmail_imap_Object.delete_messages(i)
self.label_mailinfo.setText("Process Finished")
return True
else:
# NOTHING TO DELETE
return False
def clear_text_browser(self):
self.deleted_mails.clear()
# print("self.deleted_mails.clear()")
self.textBrowser.clear()
# print("self.textBrowser.clear()")
def close_app_exit(self):
# try to logout Before EXIT
try:
if self.login():
if self.auto_detect_mail() == 'yahoo':
self.yahoo_imap_Object.logout()
# print("self.yahoo_imap_Object.logout()")
elif self.auto_detect_mail() == 'hotmail':
self.hotmail_imap_Object.logout()
# print("self.hotmail_imap_Object.logout()")
elif self.auto_detect_mail() == 'gmail':
self.gmail_imap_Object.logout()
# print("self.gmail_imap_Object.logout()")
else:
self.label_emailconfirmed.setText("First login")
# self.button_exit.clicked.connect(sys.exit(0))
# print("no internet to logout")
except:
self.button_exit.clicked.connect(sys.exit(0))
# print("exit except")
# return False
# EXIT
self.button_exit.clicked.connect(sys.exit(0))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = TheWindow()
ui.show()
sys.exit(app.exec_())
谢谢,当我在虚拟环境中运行命令时程序已完美运行,问题已解决