【问题标题】:How to show results from a Biopython Pubmed search in a Gtk.TextView?如何在 Gtk.TextView 中显示 Biopython Pubmed 搜索的结果?
【发布时间】:2016-09-11 19:14:01
【问题描述】:

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

【问题讨论】:

  • 这不是一个独立的示例,因此很难重现您的问题。您的(已注释掉的)print 语句在 for 循环内,因此如果您使用它,您将首先看到第一个条目,然后是第一个和第二个条目,依此类推?
  • 抱歉无法重现。是的,注释掉的打印语句确实有效,但是我不想打印结果,我想在 Gtk.TextView 中显示它,以便我在一个窗口中拥有所有记录。目前,Gtk.TextView 中只显示第一条记录,而不是全部。
  • 我的猜测:您的records_str 似乎是一长行(我没有看到像\n 这样的换行符)。 Gtk.TextView 的默认行为是不换行文本,因此您拥有所有条目但只看到开头。试试Gtk.TextView.set_wrap_mode() 或在每一行添加一个换行符。
  • 太好了,文本换行已经工作了,所以现在我可以看到所有的记录了。非常感谢您的帮助,并为我有点不清楚的问题道歉!

标签: python gtk biopython pubmed


【解决方案1】:

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

来自Python GTK+ 3 tutorial

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2020-07-08
    • 2016-12-11
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多