gtk.ListStore kann keine Daten hinzufügen.

Programmierung für GNOME und GTK+, GUI-Erstellung mit Glade.
Antworten
law

hallo,

ich habe gerade begonnen ein kleines tool in python zu schreiben jetzt habe ich aber das problem mit gtk.liststore. ich lese ein verzeichnis mit listdir() aus und will diese daten dann in ein ListStore einfügen aber ich bekomme immer folgenden fehler:

ValueError: row sequence has wrong length

und hier ist mein code:

Code: Alles auswählen

#!/usr/bin/env python

import gtk.glade
from os import *

accountDir = '/etc/ppp/peers/'
print 'try to get accounts from: ', accountDir
getAccountDirData = listdir(accountDir)
getAccountCount = len(getAccountDirData)


print '(',getAccountCount,')','found accounts: ', getAccountDirData

xml = gtk.glade.XML ('project1.glade')
d = xml.get_widget ('dialog1')
combobox = xml.get_widget ('combobox')

liststore = gtk.ListStore(str)
cell = gtk.CellRendererText()
combobox.pack_start(cell)
combobox.add_attribute(cell, 'text', 0)

#liststore.append(getAccountDirData[1])
#liststore.append(getAccountDirData[2])

i = 0
while i < getAccountCount:
    print 'adding ', i, ': ', getAccountDirData[i]
    #liststore.append(getAccountDirData[i])
    i += 1

combobox.set_model(liststore)

d.run()
mfg law
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Könntest du bitte eine GUI posten die entweder ohne Glade auskommt, oder die Glade-Datei mitposten?
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
law

meine fehler :(

hier die glade datei.

Code: Alles auswählen

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkDialog" id="dialog1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">dialog1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="has_separator">True</property>

  <child internal-child="vbox">
    <widget class="GtkVBox" id="dialog-vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child internal-child="action_area">
	<widget class="GtkHButtonBox" id="dialog-action_area1">
	  <property name="visible">True</property>
	  <property name="layout_style">GTK_BUTTONBOX_END</property>

	  <child>
	    <widget class="GtkButton" id="okbutton1">
	      <property name="visible">True</property>
	      <property name="can_default">True</property>
	      <property name="can_focus">True</property>
	      <property name="label">gtk-ok</property>
	      <property name="use_stock">True</property>
	      <property name="relief">GTK_RELIEF_NORMAL</property>
	      <property name="focus_on_click">True</property>
	      <property name="response_id">-5</property>
	    </widget>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">True</property>
	  <property name="pack_type">GTK_PACK_END</property>
	</packing>
      </child>

      <child>
	<widget class="GtkVBox" id="vbox1">
	  <property name="visible">True</property>
	  <property name="homogeneous">False</property>
	  <property name="spacing">0</property>

	  <child>
	    <widget class="GtkLabel" id="label1">
	      <property name="visible">True</property>
	      <property name="label" translatable="yes">label1</property>
	      <property name="use_underline">False</property>
	      <property name="use_markup">False</property>
	      <property name="justify">GTK_JUSTIFY_LEFT</property>
	      <property name="wrap">False</property>
	      <property name="selectable">False</property>
	      <property name="xalign">0.5</property>
	      <property name="yalign">0.5</property>
	      <property name="xpad">0</property>
	      <property name="ypad">0</property>
	      <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
	      <property name="width_chars">-1</property>
	      <property name="single_line_mode">False</property>
	      <property name="angle">0</property>
	    </widget>
	    <packing>
	      <property name="padding">0</property>
	      <property name="expand">False</property>
	      <property name="fill">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkComboBox" id="combobox">
	      <property name="visible">True</property>
	      <property name="add_tearoffs">False</property>
	      <property name="focus_on_click">True</property>
	    </widget>
	    <packing>
	      <property name="padding">0</property>
	      <property name="expand">True</property>
	      <property name="fill">True</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Okay, hier hast du mal eine funktionierende Version:
Änderungen:
  • Encoding latin-1 definiert
  • Keinen os * Import mehr, *-Imports sind evil. Stattdessen heißt es nun os.listdir
  • getAccountCount losgeworden, da nur einmal benötigt - Speicherverschwendung.
  • Stringformatierung mit print und Kommas gegen Formatierung mit Prozent ersetzt: mächtiger, lesbarer.
  • Dem Python Style-Guide, PEP8 angepasst. also aus xml.get_widget ('combobox') xml.get_widget('combobox') gemacht.
  • Merkwürdige while Schleife durch for-Ersetzt. Keine Javaismen mehr: Pythons for ist viel mächtiger als das von Java.
  • combobox.append_text statt liststore.append benutzt.
Und so sieht es aus:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: latin-1 -*-

import os
import gtk.glade

accountDir = '/etc/ppp/peers/'
print 'try to get accounts from: %s' % accountDir
getAccountDirData = os.listdir(accountDir)

print 'Found %d accounts: %s' % (len(getAccountDirData), getAccountDirData)

xml = gtk.glade.XML('listglade.glade')
d = xml.get_widget('dialog1')
combobox = xml.get_widget('combobox')

liststore = gtk.ListStore(str)
combobox.set_model(liststore)
cell = gtk.CellRendererText()
combobox.pack_start(cell, True)
combobox.add_attribute(cell, 'text', 0) 

for number, account in enumerate(getAccountDirData):
    print 'adding %d : %s' % (number, account)
    combobox.append_text(account)

d.run()
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
law

funktioniert jetzt super!!!

danke, werde mich jetzt wieder weiter in python vertiefen. :lol:


law
Antworten