I've looked through numerous stackoverflow questions related to my problem but can't find the solution anywhere. I'm quite new to (Bio)Python so perhaps the question has an obvious answer somewhere that I overlooked.
Problem: I'd like to display a piece of text when the user hovers a checkbutton. I create the checkbuttons using a for loop over a dictionary. However, and understandably, when I hover the checkbutton in my widget only the text for the last widget is preserved and shown, since that variable has that value, at the end of the loop. Therefore for all checkbuttons the same text is shown, which is not the intention.
What I am trying to make: using Biopython I want to make a script to check for new scientific publications in Pubmed for a certain keyword. The widget shows the titles of the past week (in this case for Parkinson's Disease). When I hover the title I would like to display the abstract. Following this I would download the articles marked by checkbuttons (meaning I think those are interesting to read).
I'm able to get the output of the checkbuttons (found that solution by browsing stackoverflow) using the dictionary and IntVar(). But how do I make sure that each checkbuttons retains its own 'binding text'?
Thanks in advance!
My code:
from Bio import Entrez
Entrez.email = "myemailadres"
from Bio import Medline
from Tkinter import *
def query_checkbuttons(articledict):
for key, value in articledict.items():
state = value[2].get()
if state != 0:
print(key)
def checkpubmed(searchterm):
articlebutton.destroy()
welcome.destroy()
handle = Entrez.esearch(db="pubmed", term=searchterm, retmax=10, reldate=7)
idlist = Entrez.read(handle)["IdList"]
handle = Entrez.efetch("pubmed", id=idlist, rettype="medline", retmode="text")
records = Medline.parse(handle)
articledict = {}
for record in records:
try:
articledict[record["PMID"]] = [record['TI'], record['AB'], 0]
except KeyError:
articledict[record["PMID"]] = [record['TI'], "Abstract not available", 0]
titleframe = Frame(mainframe).pack(side=BOTTOM)
abstractcontent = Label(mainframe, text="Hover over a title to get abstract information")
abstractcontent.pack(side=BOTTOM, anchor=W)
for key in articledict:
articledict[key][2] = IntVar()
c = Checkbutton(titleframe, text=articledict[key][0], variable=articledict[key][2])
c.bind("<Enter>", lambda event: abstractcontent.configure(text=articledict[key][1]))
c.bind("<Leave>", lambda event: abstractcontent.configure(text=""))
c.pack(anchor=W)
handle.close()
print articledict[key][1]
confirm = Button(mainframe, text="Click to confirm selection", command=lambda: query_checkbuttons(articledict))
confirm.pack(side=BOTTOM)
root = Tk()
mainframe = Frame(root)
mainframe.pack(anchor=W)
welcome = Label(mainframe, text="Are you ready to view the articles of the past week?")
welcome.pack(side=TOP)
articlebutton = Button(mainframe, text="Press for new articles", command=lambda: checkpubmed("Parkinson"))
articlebutton.pack(side=LEFT)
exitbutton = Button(mainframe, text="Press to exit", command=mainframe.quit)
exitbutton.pack(side=TOP, anchor=W)
root.mainloop()
root.destroy()