Python 3.x 实时更新treeview
这是我正在尝试的树视图Python 3.x 实时更新treeview,python-3.x,listview,gtk3,Python 3.x,Listview,Gtk3,这是我正在尝试的树视图 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 windo
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
循环的每次迭代都附加treeview,如下所示:
self.cr_liststore.append(list(api_tups[2:6]))
在i
循环中
但实际上,crrefwin
只有在i
循环完成后才会出现
我在这里做错了什么?问题很可能是您在Gtk的主循环中执行此操作,Gtk也处理显示您的项目。为了解决这个问题,Gtk主循环应该有机会渲染东西 最有可能的方法是在
for i
循环中包含以下while
:
while Gtk.events_pending():
Gtk.main_iteration_do(False)
这个小while循环基本上检查Gtk是否有挂起的东西(比如绘制附加)并循环,直到没有其他挂起的东西