How to show results from a Biopython Pubmed search in a Gtk.TextView?
<p>I want to search Pubmed using Biopython (the code is in the Biopython documentation) and display the results (title, authors, source) of each record in a Gtk.TextView. The code works when just printed but when I try to use the TextView, only the first record is shown. If anyone knows why this is the case, I'd appreciate the help.</p>

<p>Here is what I've got so far...</p>

<pre><code>def pbmd_search(self): #searches pubmed database, using Biopython documentation
    handle = Entrez.egquery(term=self.entry.get_text())
    record = Entrez.read(handle)
    for row in record["eGQueryResult"]:
        if row["DbName"]=="pubmed":
            print(row["Count"])

    handle = Entrez.esearch(db="pubmed", term=self.entry.get_text(), retmax=1000)
    record = Entrez.read(handle)
    idlist = record["IdList"]

    handle = Entrez.efetch(db="pubmed", id=idlist, rettype="medline", retmode="text")
    records = Medline.parse(handle)
    records = list(records)

    records_str = ""
    tv = Gtk.TextView()
    for record in records:
        records_str +=("title:", record.get("TI", "?"), "authors:", record.get("AU", "?"), "source:", record.get("SO", "?"), (""))
        #print(records_str)

    tv.get_buffer().set_text(str(records_str))
    tv.set_editable(False)          
    sw = Gtk.ScrolledWindow()
    sw.set_size_request(300,200)
    sw.add(tv)
    w = Gtk.Window()                                                                                                                                                        w.add(sw)
    w.show_all()
</code></pre>

<p>As written in my comment: Your <code>for</code> loop produces one long line without linebreaks, and <code>Gtk.TextView</code> does not wrap the line.</p>

<p>From the <a href="http://python-gtk-3-tutorial.readthedocs.io/en/latest/textview.html" rel="nofollow">Python GTK+ 3 tutorial</a>:</p>

<blockquote>
  <p>Another default setting of the Gtk.TextView widget is long lines of text will continue horizontally until a break is entered. To wrap the text and prevent it going off the edges of the screen call Gtk.TextView.set_wrap_mode().</p>
</blockquote>

<p>So you should either add linebreaks to your output string or use the <code>Gtk.TextView.set_wrap_mode()</code>.</p>

python gtk biopython pubmed