PyGTKã§ããªã¼ãã¥ã¼ã«ãªã¹ãã®ãã¼ã¿(ListStore)ã表示(ãã¿ã³ãããã¼ã¿ã®é çªãå ¥ãæ¿ããã»ã³ã¼ãä¾)
ãPyGTKでツリービューにリストのデータ(ListStore)を表示(ボタンからデータの順番を入れ替える・メモ)ãã®ç¶ãã
ã³ã¼ãä¾
ãä¸ã¸ãã®å¦çãè¡ããã³ãã©ã¯ãPyGTKでツリービューにリストのデータ(ListStore)を表示(ボタンからデータの順番を入れ替える・メモ)ãã§2種é¡ã®æ¹æ³ãæ±ã£ãã®ã§ããããã®æ¹æ³ã§2ã¤ç¨æãããã·ã°ãã«æ¥ç¶é¨åã§çæ¹ã³ã¡ã³ãã«ãã¦ããã®ã§ããã®é¨åãå ¥ãæ¿ããã°ãã1ã¤ã®ã»ãã使ç¨ãããã
åç¬é¸æã¢ã¼ãæ
#! /usr/bin/python # -*- encoding: utf-8 -*- import sys try: import pygtk pygtk.require('2.0') except: pass try: import gtk except: print >> sys.stderr, 'Error: PyGTK is not installed' sys.exit(1) class TreeViewWithColumn(gtk.TreeView): """ ã³ã©ã ãå«ãã ããªã¼ãã¥ã¼ """ ( COLUMN_STR, ) = range(1) def __init__(self, *args, **kwargs): gtk.TreeView.__init__(self, *args, **kwargs) # ã³ã©ã ã®è¨å® self.col_str = gtk.TreeViewColumn('Data', gtk.CellRendererText(), text=self.COLUMN_STR) # ã³ã©ã ã追å self.append_column(self.col_str) class MainWindow(gtk.Window): """ ã¡ã¤ã³ã¦ã£ã³ã㦠""" # ç´æ¥ã¦ã£ã³ãã¦ã¨ã¯é¢ä¿ãªããããã¼ã¿ã¯ä¾¿å®ä¸ããã«ç¨æãã¦ãããã¨ã«ãã data = \ [ ('AAA',), ('BBB',), ('CCC',), ('DDD',), ('EEE',), ] def __init__(self, *args, **kwargs): gtk.Window.__init__(self, *args, **kwargs) # ã·ã§ã¼ãã«ãããã¼(ã¢ã¯ã»ã©ã¬ã¼ã¿) self.accelgroup = gtk.AccelGroup() self.add_accel_group(self.accelgroup) # ã¡ãã¥ã¼é ç® self.item_quit = gtk.ImageMenuItem(gtk.STOCK_QUIT, self.accelgroup) self.menu_file = gtk.Menu() self.menu_file.add(self.item_quit) self.item_file = gtk.MenuItem('_File') self.item_file.set_submenu(self.menu_file) self.menubar = gtk.MenuBar() self.menubar.append(self.item_file) # ããªã¼ãã¥ã¼ self.treeview = TreeViewWithColumn(model=gtk.ListStore(str)) self.treeview.set_rules_hint(True) # ããªã¼ãã¥ã¼åãã¹ã¯ãã¼ã«ã¦ã£ã³ã㦠self.sw = gtk.ScrolledWindow() self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.sw.add(self.treeview) # ãã¿ã³ self.button_top = gtk.Button(stock=gtk.STOCK_GOTO_TOP) self.button_up = gtk.Button(stock=gtk.STOCK_GO_UP) self.button_down = gtk.Button(stock=gtk.STOCK_GO_DOWN) self.button_bottom = gtk.Button(stock=gtk.STOCK_GOTO_BOTTOM) # ã¬ã¤ã¢ã¦ãç¨ã³ã³ãã self.vbox_btn = gtk.VBox() # ãã¿ã³ã縦ã«ä¸¦ã¹ã self.vbox_btn.pack_start(self.button_top) self.vbox_btn.pack_start(self.button_up) self.vbox_btn.pack_start(self.button_down) self.vbox_btn.pack_start(self.button_bottom) self.hbox = gtk.HBox() # ããªã¼ãã¥ã¼ã¨ãã¿ã³ç¾¤ self.hbox.pack_start(self.sw) self.hbox.pack_start(self.vbox_btn, expand=False, fill=False) self.vbox = gtk.VBox() # å ¨ä½ self.vbox.pack_start(self.menubar, expand=False, fill=False) self.vbox.pack_start(self.hbox) # ã·ã°ãã« self.connect('delete_event', gtk.main_quit) self.item_quit.connect('activate', gtk.main_quit) self.button_top.connect('clicked', self.on_button_top_clicked) self.button_up.connect('clicked', self.on_button_up_clicked) #self.button_up.connect('clicked', self.on_button_up_clicked2) self.button_down.connect('clicked', self.on_button_down_clicked) self.button_bottom.connect('clicked', self.on_button_bottom_clicked) # ãã¼ã¿è¿½å for rec in self.data: self.treeview.get_model().append(rec) # ã¦ã£ã³ã㦠self.add(self.vbox) self.set_size_request(350, 300) def on_button_top_clicked(self, widget): """ é¸æé ç®ãä¸çªä¸ã«ç§»å """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, iter_current) = self.treeview.get_selection().get_selected() # move_after()ã®2çªç®ã®å¼æ°ãNoneã«ããã¨ä¸çªä¸ã«ç§»åãã model.move_after(iter_current, None) def on_button_up_clicked(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»å """ (model, iter_current) = self.treeview.get_selection().get_selected() # å é ã®é ç®ã示ãã¦ãããé¸æé ç®ããªãå ´åã¯ä½ãããªã if iter_current \ and model.get_path(iter_current) != model.get_path(model.get_iter_first()): # é¸æé ç®ã®1ã¤ä¸(æå)ã®é ç®ãæ¢ã # éæ¹åã«ã¯ãã©ããªãã®ã§å é ããé ã«ãã©ã iter = model.get_iter_first() while True: iter_next = model.iter_next(iter) # 1ã¤æåãé¸æé ç®ã¨åãä½ç½®ã§ããã°ããã§æ¢ã¾ã if model.get_path(iter_next) == model.get_path(iter_current): break iter = iter_next.copy() model.move_before(iter_current, iter) # ãã®ã¨ãã®iterã1ã¤æå def on_button_up_clicked2(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»åã»ãã®2 """ (model, iter_current) = self.treeview.get_selection().get_selected() if iter_current \ and model.get_path(iter_current) != model.get_path(model.get_iter_first()): # ããªã¼ãã¹ã®ã¿ãã«ã®è¦ç´ (ListStoreã§ã¯1é層ã ããªã®ã§0çªã®ã¿)ã # 1å¼ããã¨ã§æåã®é ç®ã®ããªã¼ãã¹ãå¾ã¦TreeIterã«å¤æããæ¹æ³ããã iter = model.get_iter(model.get_path(iter_current)[0] - 1) model.move_before(iter_current, iter) def on_button_down_clicked(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»å """ (model, iter_current) = self.treeview.get_selection().get_selected() # æå¾ã®é ç®ã示ãã¦ãããé¸æé ç®ããªãå ´åã¯ä½ãããªã if iter_current: iter_next = model.iter_next(iter_current) if iter_next: model.move_after(iter_current, iter_next) def on_button_bottom_clicked(self, widget): """ é¸æé ç®ãä¸çªä¸ã«ç§»å """ (model, iter_current) = self.treeview.get_selection().get_selected() # move_before()ã®2çªç®ã®å¼æ°ãNoneã«ããã¨ä¸çªä¸ã«ç§»åãã model.move_before(iter_current, None) class PyGTKTreeViewListStoreTest5: """ ãªã¹ããç¨ããããªã¼ãã¥ã¼ã®ãã¹ã5 """ def main(self): """ ã¢ããªã±ã¼ã·ã§ã³ã®ã¡ã¤ã³å¦ç """ win = MainWindow() win.show_all() gtk.main() if __name__ == '__main__': app = PyGTKTreeViewListStoreTest5() app.main()
è¤æ°é¸æã¢ã¼ãã§1ã¤ã®é ç®ãé¸æãããã¨ãã«ç§»åã§ããããã«ããå ´å
è¤æ°åã®é ç®ãé¸æãããã¨ãã«ã¯ä½ãããã1ã¤ã®é ç®ãé¸æãããã¨ãã«éãä¸ã®ããã«ç§»åã§ããããã«ãªã£ã¦ããã
#! /usr/bin/python # -*- encoding: utf-8 -*- import sys try: import pygtk pygtk.require('2.0') except: pass try: import gtk except: print >> sys.stderr, 'Error: PyGTK is not installed' sys.exit(1) class TreeViewWithColumn(gtk.TreeView): """ ã³ã©ã ãå«ãã ããªã¼ãã¥ã¼ """ ( COLUMN_STR, ) = range(1) def __init__(self, *args, **kwargs): gtk.TreeView.__init__(self, *args, **kwargs) # ã³ã©ã ã®è¨å® self.col_str = gtk.TreeViewColumn('Data', gtk.CellRendererText(), text=self.COLUMN_STR) # ã³ã©ã ã追å self.append_column(self.col_str) # è¤æ°è¡é¸æãå¯è½ã«ãã self.get_selection().set_mode(gtk.SELECTION_MULTIPLE) class MainWindow(gtk.Window): """ ã¡ã¤ã³ã¦ã£ã³ã㦠""" # ç´æ¥ã¦ã£ã³ãã¦ã¨ã¯é¢ä¿ãªããããã¼ã¿ã¯ä¾¿å®ä¸ããã«ç¨æãã¦ãããã¨ã«ãã data = \ [ ('AAA',), ('BBB',), ('CCC',), ('DDD',), ('EEE',), ] def __init__(self, *args, **kwargs): gtk.Window.__init__(self, *args, **kwargs) # ã·ã§ã¼ãã«ãããã¼(ã¢ã¯ã»ã©ã¬ã¼ã¿) self.accelgroup = gtk.AccelGroup() self.add_accel_group(self.accelgroup) # ã¡ãã¥ã¼é ç® self.item_quit = gtk.ImageMenuItem(gtk.STOCK_QUIT, self.accelgroup) self.menu_file = gtk.Menu() self.menu_file.add(self.item_quit) self.item_file = gtk.MenuItem('_File') self.item_file.set_submenu(self.menu_file) self.menubar = gtk.MenuBar() self.menubar.append(self.item_file) # ããªã¼ãã¥ã¼ self.treeview = TreeViewWithColumn(model=gtk.ListStore(str)) self.treeview.set_rules_hint(True) # ããªã¼ãã¥ã¼åãã¹ã¯ãã¼ã«ã¦ã£ã³ã㦠self.sw = gtk.ScrolledWindow() self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.sw.add(self.treeview) # ãã¿ã³ self.button_top = gtk.Button(stock=gtk.STOCK_GOTO_TOP) self.button_up = gtk.Button(stock=gtk.STOCK_GO_UP) self.button_down = gtk.Button(stock=gtk.STOCK_GO_DOWN) self.button_bottom = gtk.Button(stock=gtk.STOCK_GOTO_BOTTOM) # ã¬ã¤ã¢ã¦ãç¨ã³ã³ãã self.vbox_btn = gtk.VBox() # ãã¿ã³ã縦ã«ä¸¦ã¹ã self.vbox_btn.pack_start(self.button_top) self.vbox_btn.pack_start(self.button_up) self.vbox_btn.pack_start(self.button_down) self.vbox_btn.pack_start(self.button_bottom) self.hbox = gtk.HBox() # ããªã¼ãã¥ã¼ã¨ãã¿ã³ç¾¤ self.hbox.pack_start(self.sw) self.hbox.pack_start(self.vbox_btn, expand=False, fill=False) self.vbox = gtk.VBox() # å ¨ä½ self.vbox.pack_start(self.menubar, expand=False, fill=False) self.vbox.pack_start(self.hbox) # ã·ã°ãã« self.connect('delete_event', gtk.main_quit) self.item_quit.connect('activate', gtk.main_quit) self.button_top.connect('clicked', self.on_button_top_clicked) self.button_up.connect('clicked', self.on_button_up_clicked) #self.button_up.connect('clicked', self.on_button_up_clicked2) self.button_down.connect('clicked', self.on_button_down_clicked) self.button_bottom.connect('clicked', self.on_button_bottom_clicked) # ãã¼ã¿è¿½å for rec in self.data: self.treeview.get_model().append(rec) # ã¦ã£ã³ã㦠self.add(self.vbox) self.set_size_request(350, 300) def on_button_top_clicked(self, widget): """ é¸æé ç®ãä¸çªä¸ã«ç§»å """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, selected) = self.treeview.get_selection().get_selected_rows() iters = [model.get_iter(path) for path in selected] if len(iters) == 1: # move_after()ã®2çªç®ã®å¼æ°ãNoneã«ããã¨ä¸çªä¸ã«ç§»åãã model.move_after(iters[0], None) def on_button_up_clicked(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»å """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, selected) = self.treeview.get_selection().get_selected_rows() iters = [model.get_iter(path) for path in selected] if len(iters) == 1: # å é ã®é ç®ã示ãã¦ãããé¸æé ç®ããªãå ´åã¯ä½ãããªã if iters[0] \ and model.get_path(iters[0]) != model.get_path(model.get_iter_first()): # é¸æé ç®ã®1ã¤ä¸(æå)ã®é ç®ãæ¢ã # éæ¹åã«ã¯ãã©ããªãã®ã§å é ããé ã«ãã©ã iter = model.get_iter_first() while True: iter_next = model.iter_next(iter) # 1ã¤æåãé¸æé ç®ã¨åãä½ç½®ã§ããã°ããã§æ¢ã¾ã if model.get_path(iter_next) == model.get_path(iters[0]): break iter = iter_next.copy() model.move_before(iters[0], iter) # ãã®ã¨ãã®iterã1ã¤æå def on_button_up_clicked2(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»åã»ãã®2 """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, selected) = self.treeview.get_selection().get_selected_rows() iters = [model.get_iter(path) for path in selected] if len(iters) == 1: if iters[0] \ and model.get_path(iters[0]) != model.get_path(model.get_iter_first()): # ããªã¼ãã¹ã®ã¿ãã«ã®è¦ç´ (ListStoreã§ã¯1é層ã ããªã®ã§0çªã®ã¿)ã # 1å¼ããã¨ã§æåã®é ç®ã®ããªã¼ãã¹ãå¾ã¦TreeIterã«å¤æããæ¹æ³ããã iter = model.get_iter(model.get_path(iters[0])[0] - 1) model.move_before(iters[0], iter) def on_button_down_clicked(self, widget): """ é¸æé ç®ã1ã¤ä¸ã«ç§»å """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, selected) = self.treeview.get_selection().get_selected_rows() iters = [model.get_iter(path) for path in selected] if len(iters) == 1: # æå¾ã®é ç®ã示ãã¦ãããé¸æé ç®ããªãå ´åã¯ä½ãããªã if iters[0]: iter_next = model.iter_next(iters[0]) if iter_next: model.move_after(iters[0], iter_next) def on_button_bottom_clicked(self, widget): """ é¸æé ç®ãä¸çªä¸ã«ç§»å """ # ç¾å¨é¸æããã¦ããé ç®ãåãåºã (model, selected) = self.treeview.get_selection().get_selected_rows() iters = [model.get_iter(path) for path in selected] if len(iters) == 1: # move_before()ã®2çªç®ã®å¼æ°ãNoneã«ããã¨ä¸çªä¸ã«ç§»åãã model.move_before(iters[0], None) class PyGTKTreeViewListStoreTest5Multi: """ ãªã¹ããç¨ããããªã¼ãã¥ã¼ã®ãã¹ã5-2 è¤æ°é¸æã¢ã¼ãã§åæ°é¸ææã«ç§»åã§ããããã«ãããã® """ def main(self): """ ã¢ããªã±ã¼ã·ã§ã³ã®ã¡ã¤ã³å¦ç """ win = MainWindow() win.show_all() gtk.main() if __name__ == '__main__': app = PyGTKTreeViewListStoreTest5Multi() app.main()
ã¡ã¤ã³ã¦ã£ã³ãã¦ãªãã¸ã§ã¯ãã®æ¸ãæ¹ã®å¾®èª¿æ´ã«ã¤ãã¦
ãã®è¨äºã®å 容ã¨ã¯é¢ä¿ããªãããæè¿ã¾ã§gtk.main()ãå¼ã¶åã®ã¡ã¤ã³ã¦ã£ã³ãã¦ãªãã¸ã§ã¯ãã
win = MainWindow()
ã®ããã«çæããªãããã®ãªãã¸ã§ã¯ã(win)ã使ç¨ãã¦ããªãæ¸ãæ¹ã«ãªã£ã¦ããã®ã§ã¡ã³ãé¢æ°show_all()ãããã§å¼ã¶ãã¨ã«ãããéå»ã®è¨äºã®ä¾ã«ã¤ãã¦ãä¿®æ£ãè¡ã£ãã
é¢é£è¨äº:
- PyGTKでツリービューにリストのデータ(ListStore)を表示(概要とメモ)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(簡単な例)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・メモ)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・コード例)
- PyGTKでツリービューに階層を持ったデータ(TreeStore)を表示
- PyGTKでツリービューにリストのデータ(ListStore)を表示(項目をGUI上で追加/削除、データの並べ替えや入れ替えなど)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(モデルの内容を取り出す)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(ボタンからデータの順番を入れ替える・メモ)