Ciao a tutti,
Premetto che sono agli inizi con python e uso Gnu/Linux. Dunque:
Ho scritto un programma in python usando i moduli gtk, e visto che il file principale 'gtk_button.py' (in C detto main) è diventato troppo lungo (si fa per dire) ho deciso di mettere qualche funzione in un secondo file 'lib.py'. La funzione consente di creare una finestra e di visualizzarla. Tale funzione sarà poi richiamata dal file principale 'gtk_button.py'.
Vi posto il codice del file 'gtk_button.py' (la chiamata alla funzione si chiama 'lib_Main.make_secondwindow()' :
codice:
#test su gtk e principali widget
import pygtk
pygtk.require('2.0')
import gtk
from lib import *
i = 0 #indice di avanzamento pulsante
class Main:
def delete_event(self, widget, event, data=None):
print "destroy event occurred"
gtk.main_quit()
def button1_clicked(self, widget, data):
global i #definisco i una variabile globale
i = i + 1
if i > 9:
i = 0
print "button1 clicked - data = %d" % i
#self.label1.set_text("%d" % i)
#self.label1.set_markup('%d' % i)
self.label1.set_markup('<span foreground="blue" background="yellow">%d</span>' % i)
if i == 9:
#Creo la finestra secondaria
lib_Main.make_secondwindow()
def __init__(self):
#Creo la finestra principale
self.mainwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.mainwindow.connect("delete_event", self.delete_event)
self.mainwindow.set_title("main window")
self.mainwindow.set_position(gtk.WIN_POS_CENTER_ALWAYS)
self.mainwindow.set_border_width(10)
self.mainwindow.resize(250,250)
#Creo la hbox1
self.hbox1 = gtk.HBox(True,2)
self.mainwindow.add(self.hbox1)
self.hbox1.show()
#Creo il button1
self.button1 = gtk.Button("Press")
self.button1.connect("clicked", self.button1_clicked, None)
self.hbox1.pack_start(self.button1, True, True, 0)
self.button1.show()
#Creo la label1
self.label1 = gtk.Label("Press the button on left")
self.hbox1.pack_start(self.label1, True, True, 0)
self.label1.show()
self.mainwindow.show()
def main(self):
gtk.main()
print __name__
if __name__ == "__main__":
base = Main()
base.main()
Vi posto il codice del file 'lib.py':
codice:
import pygtk
pygtk.require('2.0')
import gtk
class lib_Main():
def make_secondwindow():
self.secondwindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.secondwindow.set_title("second window")
self.secondwindow.set_position(gtk.WIN_POS_CENTER_ALWAYS)
self.secondwindow.set_border_width(10)
self.label2 = gtk.Label("Application made by domenan, in 02/05/2009")
self.secondwindow.add(self.label2)
self.label2.show()
self.secondwindow.show()
Questo è l'errore che ricevo, quando viene avviata la funzione 'lib_Main.make_secondwindow()' :
codice:
Traceback (most recent call last):
File "gtk_button.py", line 30, in button1_clicked
lib_Main.make_secondwindow()
TypeError: unbound method make_secondwindow() must be called with lib_Main instance as first argument (got nothing instead)
sapreste aiutarmi?
Grazie