1
這是我正在嘗試的一個treeview。實時更新treeview
def search_cr(self):
headers = {'Accept': 'application/x-bibtex; charset=utf-8'}
jsonget=(urlopen(http://api.crossref.org/works?query.author=Albert+Einstein))
data = (json.loads(jsonget.read().decode()))
# First, open a window to dispaly data
self.crrefwin = Gtk.Window()
self.crrefwin.set_title("Showing search result from CrossRef")
self.crrefwin.set_default_size(950, 350)
grid = Gtk.Grid()
self.cr_liststore = Gtk.ListStore(str, str, str, str)
self.treeview = Gtk.TreeView(model=self.cr_liststore)
for i, column_title in enumerate(["Title", "Author", "Journal", "Year"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.treeview.append_column(column)
self.scrolw = Gtk.ScrolledWindow()
grid.attach(self.scrolw, 0, 0, 1, 1)
self.scrolw.add(self.treeview)
self.crrefwin.add(grid)
self.crrefwin.show_all()
#This completes `self.crrefwin`. Why it is not created here?
# Fetching data and updating to cr_liststore
for i in range(len(data["message"]["items"][0])):
url=((data["message"]["items"][i]["URL"]))
r = requests.get(url, headers=headers)
r.encoding = "utf-8"
api_tups = self.Parser.parsing_read(io.StringIO(r.text.strip()))
self.cr_liststore.append(list(api_tups[2:6]))
的問題是,我期待它追加樹狀去年i
循環的每個迭代,如:
self.cr_liststore.append(list(api_tups[2:6]))
是i
循環中。
但實際上,crrefwin
僅在i
循環完成後纔出現。
我在做什麼錯在這裏?