那是我的代码
def report_one_friend(self):
filename = QFileDialog.getSaveFileName(self, "", "cars.xlsx","Excel(.xlsx)")
if filename:
openFile = open(filename, 'r').read()
self.plainTextEdit.appendPlainText(openFile)
wb = xlsxwriter.Workbook(filename[0])
sheet1 = wb.add_worksheet()
sql = '''SELECT * FROM ahmed WHERE mth_search = %s'''
mth_search = self.lineEdit_3.text()
c = self.conn.cursor()
c.execute(sql, [(mth_search)])
data = c.fetchall()
for row in data:
print(row)
sheet1.write(0,2,'الاسم')
sheet1.write(0,0,row[1])
sheet1.write(1, 2, 'الرقم')
sheet1.write(1, 0, row[2])
wb.close()
这给了我一个错误:
Connected to MySQL database using C extension... MySQL Server version on 8.0.12
Traceback (most recent call last):
File "/Users/mahmoudtarek/Desktop/mth1/index.py", line 174, in mth_friends
self.report_one_friend()
File "/Users/mahmoudtarek/Desktop/mth1/index.py", line 208, in report_one_friend
openFile = open(filename, 'r').read()
TypeError: expected str, bytes or os.PathLike object, not tuple
答案 0 :(得分:0)
您会看到“所选文件”和“应用的过滤器”的元组。
最小代码示例:
from PyQt5.Qt import *
app = QApplication([])
print(QFileDialog.getSaveFileName())
选择“某些文件”:
('C:/scratches/scratch.py', 'All Files (*)')
但是当您不选择某些内容并取消对话框时,则两个字符串为空:('', '')
。
由于其中有两个字符串的元组为true
,因此您尝试使用此文件open()
来创建文件,这会导致错误。
解决方案:
将元组解压缩为两个变量或使用index [0],如下所示:
filename, filter = QFileDialog.getSaveFileName(self, "", "cars.xlsx","Excel(.xlsx)")
if filename:
openFile = open(filename, 'r').read()
...