函数
def clickconector
返回表单填写情况,并且 def radio_value
在作业待处理或已完成(“pendiente”或“terminado”)时返回。 clickconector
和 radio_value
* 函数编写正确,但我对 def text_file
函数有问题。
Join Tuple :生成的文件与标题具有相同的内部,如果是
radio_value
函数,则它会出现,因为它在算法中指定它是另一个函数 clickconector
。
名称中的“.txt”:标题没有按我想要的方式写。我明白了:
('Terminado', 'MT16'). txt
,(标题中写着.txt
)。
当它应该是:Terminado MT16
。另外,文件类型在我看来不是.txt
,我必须选择用鼠标这样打开它
def clickconector (self):
relleno = [self.bitacora.text(), self.turno.text(), self.asesor.text(), self.item.text(), self.modelo.text(), self.identificacion.text(), self.rig.text(),self.horometro.text(),self.condicion.text(),self.orden.text(), self.observacion.text()]
form_label = ["bitacora", 'turno', 'asesor', 'item', 'modelo', 'identificacion', 'rig', 'horometro', 'condicion', 'orden', 'observacion']
for a,b in zip (form_label, relleno):
return (a,b)
def radio_value (self):
if self.pendiente.isChecked():
return 'Pendiente' , self.bitacora.text()
if self.terminado.isChecked():
return'Terminado', self.bitacora.text()
def text_file (self):
Bloc_title= self.radio_value()
f = open(str(Bloc_title)+'. txt', 'w')
f.write(str(self.clickconector()))
return f
radio_value
的返回类型是tuple
。您要将元组转换为字符串,因此它将以 tuple
的形式打印,即 "(value_a, value_b, value_c)"
而不是 value_a value_b value_c
。您可以使用 join
将元组的每个值与空格连接起来,如下所示:
" ".join(self.radio_value())
您还在
". txt"
中写了".txt"
而不是f = open(str(Bloc_title)+'. txt', 'w')
。这就是文件名中包含 . txt
的原因。考虑到所有这些,您的 text_file
函数应该如下所示:
def text_file(self):
filename = f"{' '.join(self.radio_value())}.txt"
with open(filename, "w") as f:
f.write(" ".join(self.clickconnector()))
return f
请注意使用
with/open
而不是仅使用 open
。强烈建议使用 with/open
,以提高可读性和整体易用性。
问题2,
这里有几个小问题,您将文件扩展名设置为
. txt
这应该是 .txt
。请注意多余的空间。
你的
Bloc_title
是一个列表,所以你在标题中得到一个数组,你需要额外添加这个数组的部分,它的长度总是2,所以你可以执行以下操作,
def text_file (self):
Bloc_title = self.radio_value()
f = open(Bloc_title[0] + ' ' + str(Bloc_title[1] + '.txt', 'w')
f.write(str(self.clickconector()))
return f
请注意,可能不需要 str 调用,但我不确定
self.bitacora.text()
返回的数据类型。
评论中的回答, 如果您想要 relleno 中的所有元素,而只想要 form_label 中的第一个元素,我建议修改 clickconnector()
def clickconector (self):
relleno = [self.bitacora.text(), self.turno.text(), self.asesor.text(), self.item.text(), self.modelo.text(), self.identificacion.text(), self.rig.text(),self.horometro.text(),self.condicion.text(),self.orden.text(), self.observacion.text()]
form_label = ["bitacora", 'turno', 'asesor', 'item', 'modelo', 'identificacion', 'rig', 'horometro', 'condicion', 'orden', 'observacion']
return [form_label[0]] + relleno