FlissieT FlissieT - 1 year ago 48
Python Question

How to show results from a Biopython Pubmed search in a Gtk.TextView?

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.

Here is what I've got so far...

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":

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", "?"), (""))

sw = Gtk.ScrolledWindow()
w = Gtk.Window() w.add(sw)

Answer Source

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

From the Python GTK+ 3 tutorial:

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().

So you should either add linebreaks to your output string or use the Gtk.TextView.set_wrap_mode().