如何从python Gtk.ListStore中的文件中读取数据?

时间:2016-08-02 23:27:03

标签: python-3.x gtk3

我正在编写一个python3 / GTK程序,该程序涉及将包含遗传数据的文件添加到Gtk.ListStore,然后使用ComboBox Entry访问该数据。我已经将文件添加到列表存储区,但我正在努力解决的是如何选择文件以及如何使用这些数据。我设置组合框的相关代码是:

self.open_sequences = Gtk.ListStore(str)

class OsFrame(Gtk.Bin): #opens sequences for later use
  def __init__(self, open_sequences):

    Gtk.Bin.__init__(self)

    self.open_sequences = open_sequences

    self.builder = Gtk.Builder()
    self.builder.add_from_file("openseq-page.glade")
    self.os_box = self.builder.get_object("Open-box")
    self.add(self.os_box)

    self.combobox = self.builder.get_object("Open-cbox")
    self.comboentry = self.builder.get_object("Open-cbox-entry")
    self.filebox = self.builder.get_object("Open-file")
    self.filebox.connect("file_set", self.on_file_selected)        
    self.comboentry.connect("activate", self.enter_callback)

    renderer = Gtk.CellRendererText()
    self.combobox.pack_start(renderer, True)
    self.combobox.add_attribute(renderer, "text", 0)
    self.combobox.set_model(open_sequences)

def on_file_selected(self, entry): #opens a file and adds it to list store
    file = open(self.filebox.get_filename())
    self.open_sequences.append([self.filebox.get_filename()])

def enter_callback(self, widget): #runs function when enter pressed
    self.open_sequences.append([self.comboentry.get_text()]) #adds typed text to liststore 

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题 - 将文件名添加到liststore并使用字典来存储数据本身。