2016-09-01 40 views
0

,和我通過一些示例代碼工作: https://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#listbox蟒蛇2.7 GTK 3+列表框連接我剛剛下載gtk3 +打印活動行

下面是示例代碼:

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 

class ListBoxRowWithData(Gtk.ListBoxRow): 
    def __init__(self, data): 
     super(Gtk.ListBoxRow, self).__init__() 
     self.data = data 
     self.add(Gtk.Label(data)) 

class ListBoxWindow(Gtk.Window): 

    def __init__(self): 
     Gtk.Window.__init__(self, title="ListBox Demo") 
     self.set_border_width(10) 

     box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) 
     self.add(box_outer) 

     listbox = Gtk.ListBox() 
     listbox.set_selection_mode(Gtk.SelectionMode.NONE) 
     box_outer.pack_start(listbox, True, True, 0) 

     row = Gtk.ListBoxRow() 
     hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50) 
     row.add(hbox) 
     vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 
     hbox.pack_start(vbox, True, True, 0) 

     label1 = Gtk.Label("Automatic Date & Time", xalign=0) 
     label2 = Gtk.Label("Requires internet access", xalign=0) 
     vbox.pack_start(label1, True, True, 0) 
     vbox.pack_start(label2, True, True, 0) 

     switch = Gtk.Switch() 
     switch.props.valign = Gtk.Align.CENTER 
     hbox.pack_start(switch, False, True, 0) 

     listbox.add(row) 

     row = Gtk.ListBoxRow() 
     hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50) 
     row.add(hbox) 
     label = Gtk.Label("Enable Automatic Update", xalign=0) 
     check = Gtk.CheckButton() 
     hbox.pack_start(label, True, True, 0) 
     hbox.pack_start(check, False, True, 0) 

     listbox.add(row) 

     row = Gtk.ListBoxRow() 
     hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50) 
     row.add(hbox) 
     label = Gtk.Label("Date Format", xalign=0) 
     combo = Gtk.ComboBoxText() 
     combo.insert(0, "0", "24-hour") 
     combo.insert(1, "1", "AM/PM") 
     hbox.pack_start(label, True, True, 0) 
     hbox.pack_start(combo, False, True, 0) 

     listbox.add(row) 

     listbox_2 = Gtk.ListBox() 
     items = 'This is a sorted ListBox Fail'.split() 

     for item in items: 
      listbox_2.add(ListBoxRowWithData(item)) 

     def sort_func(row_1, row_2, data, notify_destroy): 
      return row_1.data.lower() > row_2.data.lower() 

     def filter_func(row, data, notify_destroy): 
      return False if row.data == 'Fail' else True 

     listbox_2.set_sort_func(sort_func, None, False) 
     listbox_2.set_filter_func(filter_func, None, False) 

     listbox_2.connect('row-activated', lambda widget, row: print(row.data)) 

     box_outer.pack_start(listbox_2, True, True, 0) 
     listbox_2.show_all() 

win = ListBoxWindow() 
win.connect("delete-event", Gtk.main_quit) 
win.show_all() 
Gtk.main() 

我得到「語法不正確」的錯誤

listbox_2.connect('row-activated', lambda widget, row: print(row.data)) 

這是直接從示例代碼,我完全在這裏輸了。請幫忙。

+1

'print'是在python2聲明,因此不能在拉姆達使用。本教程可能使用python3,或者'from __future__ import print_function'。 –

回答

0

您的問題,而不是進口打印功能另一種方法是使用再版()

listbox_2.connect('row-activated', lambda widget, row: repr(row.data))