I am making my first interface with Pyqt, using “designer” and Python to configure it.The function def clickconector
return the form fill and def radio_value
return if the job is pending or finished (“pendiente” or “terminado”). clickconector
and radio_value
* functions are written correctly, but I have problems with def text_file
function.
Problem #1 : For some reason the generated file has the same inside as the title, which would come if the radio_value
function, being that it specifies in the algorithm that It was the other function clickconector
.
Problem #2 : The title is not being written as I want. I get this: ('Terminado', 'MT16'). txt
, (It is writing .txt
in the title).
When it should be: Terminado MT16
. Also, the file type does not appear to me as .txt
, I have to select to open it like this with the mouse
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
Advertisement
Answer
The return type of radio_value
is a tuple
. You are converting a tuple to a string, so it is going to be printed in the form of a tuple
, i.e., "(value_a, value_b, value_c)"
and not value_a value_b value_c
. You can use join
to join each value of the tuple with a space, like so:
" ".join(self.radio_value())
You also wrote ". txt"
and not ".txt"
in f = open(str(Bloc_title)+'. txt', 'w')
. That is why . txt
was in the file name. With all of this in mind, your text_file
function should look something like this:
def text_file(self): filename = f"{' '.join(self.radio_value())}.txt" with open(filename, "w") as f: f.write(" ".join(self.clickconnector())) return f
Note the use of with/open
instead of just open
. Using with/open
is highly recommended for readability and overall ease-of-use.