如何在 Gtk.TextView 中显示来自 Biopython Pubmed 搜索的结果?

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

我想使用 Biopython 搜索 Pubmed(代码在 Biopython 文档中)并在 Gtk.TextView 中显示每条记录的结果(标题、作者、来源)。该代码在刚打印时有效,但当我尝试使用 TextView 时,仅显示第一条记录。如果有人知道为什么会这样,我将不胜感激。

这是我到目前为止得到的...

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

正如我在评论中所写:您的 for 循环产生一长行且没有换行符,并且 Gtk.TextView 不换行。

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

因此您应该在输出字符串中添加换行符或使用 Gtk.TextView.set_wrap_mode().