Problème d'import de fichier
Bonjour,
En voulant créer un assistant tout bête, je me suis retrouvé face à une erreur plutôt étrange.
Mon programme se divise en 4 fichiers :
- main.py (le lanceur)
- Window1.py (ma première fenêtre d'assistant)
- Window2.py (ma seconde fenêtre d'assistant)
- windows.ui (fichier glade)
La première fenêtre s'affiche, je clique sur 'Suivant'.
La seconde fenêtre s'affiche pendant que la première est détruite.
Et là, je clique sur 'Précédent' pour revenir sur la première fenêtre :
Code:
1 2 3
| Window2.py, line 27, in button_prev_clicked
window1 = Window1(self.gtkfile)
NameError: global name 'Window1' is not defined |
Si quelqu'un voit d'où ça peut venir...
D'avance merci
main.py
Code:
1 2 3 4 5 6 7 8
|
import sys
from Window1 import *
if __name__ == "__main__":
window = Window1(sys.path[0]+"/windows.ui")
gtk.main() |
Window1.py
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
import gtk, pygtk
from Window2 import *
class Window1:
def __init__(self, gtkfile):
self.gtkfile = gtkfile
builder = gtk.Builder()
builder.add_objects_from_file(gtkfile, ["window1"])
self.gtk_window = builder.get_object("window1")
signals = { "button_cancel_clicked" : self.button_cancel_clicked,
"button_next_clicked" : self.button_next_clicked,
"delete_event" : self.delete_event}
builder.connect_signals(signals)
self.gtk_window.show_all()
def button_cancel_clicked(self, button):
print("button cancel")
gtk.main_quit()
def button_next_clicked(self, button):
print("button next")
window2 = Window2(self.gtkfile)
self.gtk_window.destroy()
def delete_event(self, widget, event):
gtk.main_quit() |
Window2.py
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
import gtk, pygtk
from Window1 import *
class Window2:
def __init__(self, gtkfile):
self.gtkfile = gtkfile
builder = gtk.Builder()
builder.add_objects_from_file(gtkfile, ["window2"])
self.gtk_window = builder.get_object("window2")
signals = { "button_cancel_clicked" : self.button_cancel_clicked,
"button_prev_clicked" : self.button_prev_clicked,
"button_next_clicked" : self.button_next_clicked,
"delete_event" : self.delete_event}
builder.connect_signals(signals)
self.gtk_window.show_all()
def button_cancel_clicked(self, button):
print("button cancel")
gtk.main_quit()
def button_next_clicked(self, button):
print("button next")
gtk.main_quit()
def button_prev_clicked(self, button):
print("button prev")
window1 = Window1(self.gtkfile)
self.gtk_window.destroy()
def delete_event(self, widget, event):
gtk.main_quit() |
windows.ui
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.20"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window2">
<property name="border_width">5</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="spacing">5</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<property name="spacing">5</property>
<child>
<object class="GtkButton" id="button_cance">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="button_cancel_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_prev">
<property name="label">gtk-go-back</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="button_prev_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_nex">
<property name="label">gtk-go-forward</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="button_next_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkWindow" id="window1">
<property name="border_width">5</property>
<child>
<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="spacing">5</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="spacing">5</property>
<child>
<object class="GtkButton" id="button_cancel">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="button_cancel_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_next">
<property name="label">gtk-go-forward</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="button_next_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface> |