[英]Python - Gtk.TreeView with Gtk.ListStore get selected index
[英]gtk.TreeView does not change after changing its gtk.ListStore:
二手:python 2.79,gtk 2.0
尊敬的python和PyGtk用戶
在我的程序中,我想提供使用代碼中的方法在gtk.TreeView中顯示出色數據文件的搜索結果的機會。 在創建ListStore的函數中,我還創建了商店正在使用的csv文件。 現在我可以看到,使用不同的關鍵字進行新搜索后,csv文件已更改,但是TreeView卻沒有,盡管我每次self.search運行時都會清除存儲,但它仍然保留第一次搜索的結果。 如果您需要為每個新搜索重新啟動程序,那確實很令人興奮。
我已經嘗試過一些事情,例如為商店的datacreatin創建適當的功能,但是沒有任何作用,事實是我沒有太多的主意...
我在互聯網上發現的與此有關的所有內容都是關於使用計時器或其他更深刻的主題自動刷新商店的,似乎每個人(而不是我)都知道該怎么做...
誰能告訴我,我犯了什么錯誤? 我如何刷新TreeView?(如您所見,self.search由按鈕調用...),這里是代碼的相關部分:
import gtk, csv
class BASE:
#...
#some other funtions, irrelevant for the problem
#...
def search(self, widget, event, data=None):
#...
#all the searchingstuff and the creation of 'inwrite.csv'
#...
with open('/home/emil/Documents/inwrite.csv', 'r') as storefile:
lines = storefile.readlines()
store = gtk.ListStore(str, str, str, str, str, str)
store.clear()
i=0
while i < len(lines):
line = [lines[i]]
csvfile = csv.reader(line, delimiter=',')
for row in csvfile:
temp = (row[0], row[1], row[2], row[3], row[4], row[5])
store.append(temp)
i = i + 1
self.tabview = gtk.TreeView(store)
self.tabview.set_rules_hint(True)
self.tabview.set_reorderable(True)
self.sw.add(self.tabview)
self.tabview.show()
self.create_columns(self.tabview)
def __init__(self):
#...
#creating the Window, entries,...
#...
self.button1 = gtk.Button('Buscar')
self.button1.connect('clicked', self.search, 'button 1')
#...
#packing and showing the whole GUI
#...
def create_columns(self, tabview):
rendererText = gtk.CellRendererText()
rendererText.props.wrap_width = 80
rendererText.props.wrap_mode = gtk.WRAP_WORD
column = gtk.TreeViewColumn('Codigo', rendererText, text=0)
column.set_sort_column_id(0)
self.tabview.append_column(column)
#...
#creating 5 more columns by the same principle
#...
def main(self):
gtk.main()
if __name__=='__main__':
base = BASE()
run = base
run.main()
感謝您的幫助,告訴我是否需要更多信息!
我不知道您的程序到底有什么問題(如果您想知道,您應該提供一個可行的最小示例)。
但是,您不需要手動更新TreeView,因為TreeView總是顯示ListStore或TreeStore的內容。 您也不必每次都創建ListStore。 您可以一次創建它,並在發出事件時清除並插入新條目。
這是一個顯示其工作原理的小示例:
#from gi.repository import Gtk # uncomment to use PyGObject
import gtk as Gtk
class TestCase:
def __init__(self):
win = Gtk.Window()
button = Gtk.Button('Show')
button.connect('clicked', self.on_button_clicked)
self.clicked = 0
self.liststore = Gtk.ListStore(int)
self.liststore.append((self.clicked,))
view = Gtk.TreeView(self.liststore)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('demo', renderer, text=0)
view.append_column(column)
box = Gtk.HBox()
box.pack_start(button, True, True, 0)
box.pack_start(view, True, True, 0)
win.add(box)
win.connect('delete-event', Gtk.main_quit)
win.show_all()
def on_button_clicked(self, button):
self.clicked += 1
self.liststore.clear()
self.liststore.append((self.clicked,))
if __name__ == "__main__":
TestCase()
Gtk.main()
看來您首先創建了csv文件,然后再閱讀。 我對程序的其余部分一無所知,但是直接使用數據並將其插入ListStore而不是從csv文件中讀取數據可能更好。 甚至有可能只是刪除並插入新條目,而不清除整個ListStore並再次插入所有內容。 數據量更大時,效率會更高。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.