【发布时间】: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