0

我想使用 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()
4

1 回答 1

1

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

来自Python GTK+ 3 教程

Gtk.TextView 小部件的另一个默认设置是长行文本将水平继续,直到输入中断。要包裹文本并防止它离开屏幕边缘,请调用 Gtk.TextView.set_wrap_mode()。

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

于 2016-09-18T18:33:20.523 回答