如果我直接从Python运行它,我有一个完美运行的应用程序。但是,在我使用py2exe将其转换为可执行文件后,编码“中断”。
我运行一些从mysql服务器获取字符串“Comitê”的代码。 如果我运行代码:
fromMySql = getFromMysql()
local = "Comitê"
print(fromMySql)
print(local)
保存两个文件,一个直接从python运行,另一个用py2exe编译后,得到以下结果:
FROM PYTHON:
USING SUBLIME TEXT 3 -> REOPEN WITH ENCODING -> UTF-8:
Comit?
Comit?
USING SUBLIME TEXT 3 -> REOPEN WITH ENCODING -> WESTERN (ISO 8859-1):
Comitê
Comitê
AFTER PY2EXE:
USING SUBLIME TEXT 3 -> REOPEN WITH ENCODING -> UTF-8:
Comit?
Comitê
USING SUBLIME TEXT 3 -> REOPEN WITH ENCODING -> WESTERN (ISO 8859-1):
Comitê
Comitê
值得注意的是,在我所有文件的开头,我有:
# -*- coding: utf-8 -*-
代码(删除不重要的位):
主窗口代码:
# -*- coding: utf-8 -*-
import sys
import PySide.QtGui as QtGui
from ui.mainWindowComite_ui import Ui_MainWindowComite
import login
import connector as con
class MainWindow(QtGui.QMainWindow, Ui_MainWindowComite):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindowComite.setupUi(self, self)
self.actionCadastro.triggered.connect(self.abrirCadastro)
self.actionDelegacao.triggered.connect(self.abrirDelegacao)
self.actionAcompanhamento.triggered.connect(self.abrirAcompanhamento)
self.actionInvestigacao.triggered.connect(self.abrirInvestigacao)
self.actionHistorico.triggered.connect(self.abrirHistorico)
self.login()
def login(self):
self.loginDialog = login.Login(self)
self.loginDialog.setModal(True)
self.loginDialog.show()
self.loginDialog.rejected.connect(self.close)
self.loginDialog.accepted.connect(self.autenticado)
def autenticado(self):
self.setor = self.loginDialog.setor
print(self.setor)
print("Comitê")
if self.setor != "Comitê":
QtGui.QMessageBox.information(self, u"Erro", u"Esta versão do programa só pode ser utilizada pelo Comitê")
self.close()
def closeEvent(self, event):
con.close()
event.accept()
if __name__ == '__main__':
print("testê")
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
登录类:
import PySide.QtGui as QtGui
import sys
from ui.login_ui import Ui_Login
import connector as con
class Login(QtGui.QDialog, Ui_Login):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Login.setupUi(self, self)
def accept(self, *args, **kwargs):
self.setor = con.autenticar(self.setorLineEdit.text(),
self.senhaLineEdit.text())
if self.setor is not None: self.setor = self.setor["setor"]
if self.setor is not None:
QtGui.QDialog.accept(self)
else:
QtGui.QMessageBox.information(self, "Falha", "Tente novamente, setor ou senha errada")
连接器类:
# -*- coding: utf-8 -*-
import datetime
import pymysql
def autenticar(login, senha):
query = "Select setor FROM setores " \
"WHERE login='%s' AND senha='%s'" % (login, senha)
execute(query)
setor = cur.fetchall()
return setor[0] if len(setor)>0 else None
def connect():
global cur, con
con = pymysql.connect('localhost', 'root', '', 'database', autocommit=True)
cur = con.cursor(pymysql.cursors.DictCursor)
def execute(query):
try:
cur.execute(query)
except Exception as e:
if str(e)[1:5] == '2013': # Lost connection to server
cur.close()
con.close()
connect()
cur.execute(query)
else:
raise e
def close():
cur.close()
con.close()
connect()