Affichage d'un fichier dans un textview

Résolu
ly2008 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention   -  
Scalpweb Messages postés 1467 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, a tous
comment afficher le contenu de tout un fichier dans un textview a partir d'une commande.pour l'instant mon script que voici n'affiche que la derniere ligne.Merci a tous.

require 'gtk2'

def affcmd(str)
fichier = File.open("test/test.txt", "r")
i = 1
fichier.each_line { |ligne|

buffer.text= " - #{ligne} "
i += 1
}
fichier.close
end
def ajout()
end
def modif()
end
def supprime()
end
def startserver()
end


Gtk.init
window = Gtk::Window.new
window.set_title('Administrateur reseau')
window.signal_connect('destroy') { Gtk.main_quit }
window.set_size_request( 500, 500 )

# une boite verticale qui contient 3 rangées le menu la zone de texte et le bouton quitter

vb = Gtk::VBox.new(false,0)

#menu

menubar = Gtk::MenuBar.new
vb.pack_start( menubar, false, false, 0)
top_menu = Gtk::MenuItem.new( "menu")
menubar.append( top_menu )

#zone de texte

scrolled_window = Gtk::ScrolledWindow.new( nil, nil )
scrolled_window.border_width=( 10 )
scrolled_window.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS )
vb.pack_start( scrolled_window, true, true, 0 )



class Texte < Gtk::TextView
def affcmd
fichier = File.open("test/commande.txt", "r")
i = 1
fichier.each_line { |ligne|
buffer.text = " #{i} - #{ligne} "
i += 1
}
fichier.close
end
end


f = Texte.new
buffer=f.buffer
str=buffer.text
# buffer.text= "bienvenue"
f.affcmd



scrolled_window.add_with_viewport( f )

#bouton fermer

ok = Gtk::Button.new(Gtk::Stock::CLOSE)
ok.signal_connect('clicked') { Gtk.main_exit }
ok.set_flags( Gtk::Widget::CAN_DEFAULT )
ok.grab_default
vb.pack_start( ok, false, false, 0 )

window.add(vb)
window.show_all

Gtk.main
A voir également:

3 réponses

Scalpweb Messages postés 1467 Date d'inscription   Statut Membre Dernière intervention   43
 
Essayes de remplacer la ligne :

buffer.text= " - #{ligne} "

Par :

buffer.text= buffer.text " - #{ligne} "

Ou bien :

buffer.text= "#{buffer.text} - #{ligne} "

C'est quoi comme langage ça ? J'avais jamais vu un code comme ça...
Enfin bon ,apparemment, ton problème vient du fait que à chaque ligne de ton fichier, tu remplaces le contenu de ton buffer par la ligne, alors qu'il faudra que tu ajoutes la ligne à la fin du buffer.
0
ly2008 Messages postés 4 Date d'inscription   Statut Membre Dernière intervention  
 
merci ca marche.....
maintenent j'aimerais savoir comment installer mysql pour ruby
gé essayé ruby install mysql mé aprés il y'a probleme de configuration.......
.
0
Scalpweb Messages postés 1467 Date d'inscription   Statut Membre Dernière intervention   43
 
Je te conseilles d'ouvrir un autre poste sur le forum, car je ne peux pas t'aider. Mais avant, bascule le statut de ce topic en tant que "Résolu".
0