PyGTKã§ããªã¼ãã¥ã¼ã«ãªã¹ãã®ãã¼ã¿(ListStore)ã表示(ã¢ãã«ã®å 容ãåãåºã)
ããã§ã¯PyGTKã®ListStoreå½¢å¼ã®ãã¼ã¿ããå 容ãåãåºãæé ã«ã¤ãã¦ã主ã«æ±ãã
åãåºãæ¹
åºæ¬çã«ã¯gtk.TreeModelオブジェクトã®ã¡ã³ãé¢æ°get_value()ãããã¯get()ãç¨ãã¦ãåãåºãããé
ç®ãæã示ãgtk.TreeIterオブジェクトã渡ããã¨ã«ãªããåè
ã¯ã³ã©ã çªå·ã渡ãã¦ãã®ã³ã©ã é
ç®ã ããåãåºãæ¹æ³ã§ãå¾è
ã¯è¤æ°ã®ã³ã©ã çªå·ãå¯å¤é·å¼æ°ã§æ¸¡ãã¦ã¿ãã«ã¨ãã¦ããããã®ã³ã©ã é
ç®ã®å
容ãåãåºãå½¢ã¨ãªãã
ãã¼ã¿ãåãåºãã®ã«å¿
è¦ã¨ãªãgtk.TreeIterオブジェクトãå¾ãã«ã¯å¹¾ã¤ãã®æ¹æ³ãããããPyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・コード例)ãããPyGTKでツリービューにリストのデータ(ListStore)を表示(項目をGUI上で追加/削除、データの並べ替えや入れ替えなど)ãã§ãã§ã«ããªã¼ãã¥ã¼ä¸ã®é¸æç¯å²ããå¾ãæ¹æ³ãã³ã¼ãä¸ã«æ¸ãã¦ããã
æ¬è¨äºã«ããã¦ã¯ãä»ã®æ¹æ³ã¨ãã¦ä»¥ä¸ã®2ã¤ã®æ¹æ³ã試ãã¦ããã
ListStoreã®å ¨ã¦ã®ãã¼ã¿ãé çªã«åãåºã
gtk.TreeModelオブジェクトã®ã¡ã³ãé¢æ°get_iter_first()ã§å é ã®é ç®ãæã示ãgtk.TreeIterオブジェクトãå¾ãå¾ãwhileæã®ã«ã¼ãå ã§åé ç®ãåãåºããã«ã¼ãå ã®æå¾ã«iter_next()ãè¨è¿°ãã(æå¾ã¾ã§å°éããã¨Noneãè¿ãããã«ã¼ããæãã)ã
é ç®çªå·ã«ããä»»æã®ãã¼ã¿ãåãåºã
gtk.TreeModelオブジェクトã®ã¡ã³ãé¢æ°get_iter()ã«ãããªã¼ãã¹ã¨å¼ã°ããå½¢å¼ã®ãã¼ã¿ã渡ãã
ããªã¼ãã¹ã¯ããªã¼ã¢ãã«ã«ãããä»»æã®é
ç®ãæå®ããããã®è¡¨è¨ã§ãã¿ãã«ãããã¯æååã®å½¢ãã¨ãã
ãããã®ã¨ãã«ãã¼ã¿ã®ç¯å²å¤ã®å¤ãæå®ãããå ´åã¯ä¾å¤ValueErrorãçºçããã
http://www.pygtk.org/pygtk2tutorial/sec-TreeModelInterface.html#sec-TreePaths
ãåç
§ã
ãã®ä»ã¡ã¢(ã©ã¸ãªãã¿ã³)
ã©ã¸ãªãã¿ã³ã¯gtk.RadioButtonオブジェクトã¨ãªããã°ã«ã¼ãã®æå®ã«ã¤ãã¦ã¯ä»¥ä¸ã®ããã«ãªãã
- æå®ãªã(ãããã¯group=Noneæå®): æ°ããã°ã«ã¼ãã®å é ã®ã©ã¸ãªãã¿ã³ã¨ãªã
- groupæå®: æå®ããgtk.RadioButtonオブジェクトã®ã°ã«ã¼ãã«å«ã¾ããã©ã¸ãªãã¿ã³ã¨ãªã
å¤ã¯gtk.ToggleButtonオブジェクトã®ã¡ã³ãé¢æ°get_active()ã§åå¾ããã
ã³ã¼ãä¾
ãPyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・コード例)ãã®sexmark.pyãå¥éå¿
è¦ã
ä¾ã«ãã£ã¦ããã®ä¸ã§ä½¿ç¨ããååã¯æ¶ç©ºã®ãã®ã§ãããå®å¨ã®å人åãå£ä½åãªã©ã¨ä¸è´ãããã®ããã£ãã¨ãã¦ãé¢ä¿ã¯ãªãã
#! /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) try: import sexmark except: print >> sys.stderr, 'Error: sexmark.py required' sys.exit(1) class TreeViewWithColumn(gtk.TreeView): """ ã³ã©ã ãå«ãã ããªã¼ãã¥ã¼ """ ( COLUMN_NUM, COLUMN_SEX, COLUMN_FAMILY, COLUMN_GIVEN, ) = range(4) def __init__(self, *args, **kwargs): gtk.TreeView.__init__(self, *args, **kwargs) # ã³ã©ã ã®è¨å® self.col_num = gtk.TreeViewColumn('No.', gtk.CellRendererText(), text=self.COLUMN_NUM) self.col_sex = gtk.TreeViewColumn('Sex', gtk.CellRendererPixbuf(), pixbuf=self.COLUMN_SEX) self.col_family = gtk.TreeViewColumn('Family name', gtk.CellRendererText(), text=self.COLUMN_FAMILY) self.col_given = gtk.TreeViewColumn('Given name', gtk.CellRendererText(), text=self.COLUMN_GIVEN) # ã³ã©ã ã追å self.append_column(self.col_num) self.append_column(self.col_sex) self.append_column(self.col_family) self.append_column(self.col_given) class SexIcon: """ æ§å¥ã®ã¢ã¤ã³ã³ãã¼ã¿ã®Pixbufãä¿æ åãã¼ã¿ã¯ã[æ¬ã¯ã©ã¹å].[ã¡ã³ãå¤æ°]ãã§åãåºã """ male = gtk.gdk.pixbuf_new_from_xpm_data(sexmark.male_icon_xpm) female = gtk.gdk.pixbuf_new_from_xpm_data(sexmark.female_icon_xpm) class MainWindow(gtk.Window): """ ã¡ã¤ã³ã¦ã£ã³ã㦠""" # ç´æ¥ã¦ã£ã³ãã¦ã¨ã¯é¢ä¿ãªããããã¼ã¿ã¯ä¾¿å®ä¸ããã«ç¨æãã¦ãããã¨ã«ãã data = \ [ (1, SexIcon.male, 'Tanaka', 'Ichiro'), (2, SexIcon.female, 'Yamana', 'Hanako'), (3, SexIcon.male, 'Urashima', 'Saburo'), (4, SexIcon.male, 'Kurusu', 'Santa'), (5, SexIcon.male, 'Handa', 'Fuuta'), (6, SexIcon.female, 'Umeno', 'Tsubomi'), (7, SexIcon.male, 'Yoshi', 'Yaruzo'), (8, SexIcon.female, 'Kawai', 'Nuko'), (9, SexIcon.male, 'Hoshi', 'Kintaro'), (10, SexIcon.female, 'Shirayuki', 'Himeko'), (11, SexIcon.female, 'Ashigaka', 'Yui'), (12, SexIcon.female, 'Ageyanagi', 'Masako'), (13, SexIcon.male, 'Torino', 'Kenta'), (14, SexIcon.male, 'Kubota', 'Mochio'), (15, SexIcon.female, 'Kuroi', 'Sora'), (16, SexIcon.male, 'Hirai', 'Shin'), (17, SexIcon.female, 'Akai', 'Midori'), (18, SexIcon.female, 'Nakano', 'Anko'), (19, SexIcon.male, 'Imai', 'Takeo'), (20, SexIcon.male, 'Kouno', 'Torio'), (21, SexIcon.male, 'Yoshino', 'Yasu'), (22, SexIcon.male, 'Komatsu', 'Taro'), (23, SexIcon.male, 'Kondo', 'Musashi'), (24, SexIcon.male, 'Ono', 'Ken'), (25, SexIcon.male, 'Mochida', 'Usuichi'), (26, SexIcon.female, 'Mochida', 'Kineko'), (27, SexIcon.female, 'Honma', 'Kayo'), (28, SexIcon.male, 'Matsuno', 'Sarunosuke'), (29, SexIcon.female, 'Nishi', 'Minami'), (30, SexIcon.female, 'Usui', 'Hikaru'), (31, SexIcon.male, 'Sato', 'Toshio'), (32, SexIcon.male, 'Doi', 'Tsubasa'), (33, SexIcon.female, 'Ishimaru', 'Denko'), (34, SexIcon.female, 'Usami', 'Mimi'), (35, SexIcon.male, 'Hattori', 'Shinobu'), (36, SexIcon.female, 'Kago', 'Yuri'), (37, SexIcon.male, 'Takeda', 'Ingen'), (38, SexIcon.male, 'Kai', 'Dankichi'), (39, SexIcon.male, 'Okusa', 'Ben'), (40, SexIcon.male, 'Hara', 'Tatsuo'), (41, SexIcon.female, 'Mizuno', 'Shizuku'), (42, SexIcon.female, 'Baba', 'Nana'), ] 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(int, gtk.gdk.Pixbuf, str, 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) # ã©ã¸ãªãã¿ã³é ç® # groupãæå®ããå ´åã対象ã®ã©ã¸ãªãã¿ã³ã®ã°ã«ã¼ãã«å ¥ã # å¤ã¯gtk.ToggleButtonã®get_active()ã§åå¾ self.radio_all = gtk.RadioButton(label='Get all items') self.radio_num = gtk.RadioButton(group=self.radio_all, label='Get a single item:') # ãã¼ã¿åãåºããå®è¡ãããã¿ã³ self.button = gtk.Button('Get data') # åãåºãé ç®ãæå®ããçªå·ãå ¥åããã¹ãã³ãã¿ã³ self.spinbtn_path = gtk.SpinButton(gtk.Adjustment(0, 0, 999, 1, 0, 0)) # ã¬ã¤ã¢ã¦ãç¨ã³ã³ãã self.hbox_num = gtk.HBox() self.hbox_num.pack_start(self.radio_num, expand=False, fill=False) self.hbox_num.pack_end(self.spinbtn_path, expand=False, fill=False) self.vbox = gtk.VBox() self.vbox.pack_start(self.menubar, expand=False, fill=False) self.vbox.pack_start(self.sw) self.vbox.pack_start(self.radio_all, expand=False, fill=False) self.vbox.pack_start(self.hbox_num, expand=False, fill=False) self.vbox.pack_start(self.button, expand=False, fill=False) # ã·ã°ãã« self.connect('delete_event', gtk.main_quit) self.item_quit.connect('activate', gtk.main_quit) self.button.connect('clicked', self.on_button_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_clicked(self, widget): """ ãã¿ã³ãã¯ãªãã¯ãããã¨ãã®å¦ç """ model = self.treeview.get_model() if self.radio_all.get_active(): # å ¨ã¦ã®ãã¼ã¿ãåãåºãã¦è¡¨ç¤ºãããã¨ã«ãã iter = model.get_iter_first() while iter: # gtk.TreeModelã®get()ã§ã¯è¤æ°ã®ã³ã©ã ã®ãã¼ã¿ãä¸æ¬ã§ã¿ãã«ã¨ãã¦åãåºãã (num, sexicon, familyname, givenname) = model.get(iter, self.treeview.COLUMN_NUM, self.treeview.COLUMN_SEX, self.treeview.COLUMN_FAMILY, self.treeview.COLUMN_GIVEN) if sexicon == SexIcon.male: sex = 'Male' else: sex = 'Female' print 'Num: %d\nSex: %s\nFamily name: %s\nGiven name: %s' % \ (num, sex, familyname, givenname) iter = model.iter_next(iter) # 次ãç¡ããã°Noneã§ã«ã¼ããæãã else: # æå®ãããçªå·ã®ãã¼ã¿ãåãåºãã¦è¡¨ç¤ºãããã¨ã«ãã # ãã¹ã¯ä½çªç®ã®ãã¼ã¿ããæ´æ°ã§è¡¨ãããã®ã並ã¹ãã¿ãã«ã¨ãªã # (ListStoreã®å ´åã¯æ´æ°ã ãã§ãOK) # ä»ã«æåå表è¨ã®ãã¹ããã # http://www.pygtk.org/pygtk2tutorial/sec-TreeModelInterface.html#sec-TreePaths ãåç § path = (int(self.spinbtn_path.get_value()),) try: iter = model.get_iter(path) except ValueError: # ç¯å²å¤ã®å¤ãæå®ããTreeIterãå¾ãããªãå ´å print '(invalid tree path)' return (num, sexicon, familyname, givenname) = model.get(iter, self.treeview.COLUMN_NUM, self.treeview.COLUMN_SEX, self.treeview.COLUMN_FAMILY, self.treeview.COLUMN_GIVEN) if sexicon == SexIcon.male: sex = 'Male' else: sex = 'Female' print 'Num: %d\nSex: %s\nFamily name: %s\nGiven name: %s' % \ (num, sex, familyname, givenname) class PyGTKTreeViewListStoreTest4: """ ãªã¹ããç¨ããããªã¼ãã¥ã¼ã®ãã¹ã4 """ def main(self): """ ã¢ããªã±ã¼ã·ã§ã³ã®ã¡ã¤ã³å¦ç """ win = MainWindow() win.show_all() gtk.main() if __name__ == '__main__': app = PyGTKTreeViewListStoreTest4() app.main()
ãGet all itemsãã®ã©ã¸ãªãã¿ã³ãé¸æããã¦ããç¶æ ã§ãã¿ã³ãæ¼ãã¨
Num: 1 Sex: Male Family name: Tanaka Given name: Ichiro Num: 2 Sex: Female Family name: Yamana Given name: Hanako (ä¸ç¥) Num: 42 Sex: Female Family name: Baba Given name: Nana
ã®ããã«ãªã¹ãä¸ã®å
¨ã¦ã®é
ç®ãåã³ã©ã é
ç®ãã¨ã«ç«¯æ«ã«è¡¨ç¤ºããããGet a single itemãã®ã©ã¸ãªãã¿ã³ãé¸æããã¦ããç¶æ
ã§ãã¿ã³ãæ¼ãã¨ãå
é¨çãªé
ç®çªå·(0ããå§ã¾ã)ããã®çªå·ã®é
ç®ã表示ãããã
ä¾ãã°ãã2ããå
¥åãã¦ãã¿ã³ãæ¼ãã¨
Num: 3 Sex: Male Family name: Urashima Given name: Saburo
ã®ããã«è¡¨ç¤ºãããã
ãã¼ã¿ç¯å²å¤ã®å¤ãæå®ãããã¨ãã«ã¯
(invalid tree path)
ã¨è¡¨ç¤ºãããã
é¢é£è¨äº:
- PyGTKでツリービューにリストのデータ(ListStore)を表示(概要とメモ)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(簡単な例)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・メモ)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(データを変更可能にする・コード例)
- PyGTKでツリービューに階層を持ったデータ(TreeStore)を表示
- PyGTKでツリービューにリストのデータ(ListStore)を表示(項目をGUI上で追加/削除、データの並べ替えや入れ替えなど)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(ボタンからデータの順番を入れ替える・メモ)
- PyGTKでツリービューにリストのデータ(ListStore)を表示(ボタンからデータの順番を入れ替える・コード例)
åèURL:
- PyGTK リファレンス: gtk.TreeModel
- PyGTK リファレンス: gtk.RadioButton
- PyGTK リファレンス: gtk.ToggleButton
- http://www.pygtk.org/pygtk2tutorial/sec-TreeModelInterface.html#sec-TreePaths - ããªã¼ãã¹ã®æå®ã«ã¤ãã¦ã®åè