我想使用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()
答案 0 :(得分:1)
正如我的评论中所写:您的for
循环产生一条没有换行符的长行,Gtk.TextView
不会换行。
Gtk.TextView小部件的另一个默认设置是长行文本将水平继续,直到输入中断为止。要包装文本并防止它离开屏幕边缘,请调用Gtk.TextView.set_wrap_mode()。
因此,您应该在输出字符串中添加换行符或使用Gtk.TextView.set_wrap_mode()
。