import doesn't work as i want

  • Thread starter Olivier Noblanc ATOUSOFT
  • Start date
O

Olivier Noblanc ATOUSOFT

Hello,

In the botom of this post you will see my source code.

The problem is when i launch main.py that doesn't make anything why ?

Thanks
olivier noblanc
Atousoft
http://www.logiciel-erp.fr



I have main.py :

---------------------------------
import wx
import inc.importlist
----------------------------------

inc/importlist.py
------------------------------------
import wx.grid
import getdata
import time
import sys
-----------------------------------

inc/wxgrid.py
-------------------------------------
# -*- coding: cp1252 -*-


#starta = time.time()
import wx
import getdata

class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))

self.__set_properties()
self.__do_layout()
# end wxGlade

def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.SetSize((400, 400))
# end wxGlade

self.grid_1.CreateGrid(len(db.data), len(db.fields))

# met les labels des colonnes
index = 0
for item in db.fields:
self.grid_1.SetColLabelValue(index, item[0])
index += 1

# remplissage des données.
for row in range(len(db.data)):
for col in range(len(db.data[row])):
values = db.data[row][col]
self.grid_1.SetCellValue(row,col,str(values))

# mise en forme et affichage
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.grid_1, 1, wx.EXPAND, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
self.Layout()
# end wxGlade

# end of class MyFrame


if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
#startb = time.time() - starta
#print startb
app.MainLoop()


---------------------------------------

inc/getdata.py
----------------------------------------------
import MySQLdb


class Eb_db:
def __init__(self):
try:
connection = MySQLdb.connect(host="dellced", user="ats",
passwd="", db="atsmedicdatabase" )
cursor = connection.cursor()
cursor.execute( "SELECT * FROM PATIENT " )
except MySQLdb.OperationalError, message:
errorMessage = "Error %d:\n%s" % ( message[ 0 ], message[ 1 ] )
return
else:
self.data = cursor.fetchall()
self.fields = cursor.description
cursor.close()
connection.close()


-----------------------------------------
 
F

Fredrik Lundh

Olivier said:
In the botom of this post you will see my source code.

The problem is when i launch main.py that doesn't make anything why ?

the "if __name__" statement checks the name of the module. if you run
Python file as a script, by passing a filename to the python interpreter, the
__name__ variable is set to "__main__". if you import a file as a module,
the __name__ is the name of the module, not "__main__".

if you want main.py to do something, move that code to main.py (or move
it into a function, and call it from main.py)

</F>
 
O

Olivier Noblanc ATOUSOFT

how to move in function ?

Fredrik Lundh said:
the "if __name__" statement checks the name of the module. if you run
Python file as a script, by passing a filename to the python interpreter,
the
__name__ variable is set to "__main__". if you import a file as a
module,
the __name__ is the name of the module, not "__main__".

if you want main.py to do something, move that code to main.py (or move
it into a function, and call it from main.py)

</F>
 
F

Fredrik Lundh

Olivier said:
how to move in function ?

how to put code in a function? the same way you put code in a method.

change

if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
#startb = time.time() - starta
#print startb
app.MainLoop()

to

def main():
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
#startb = time.time() - starta
#print startb
app.MainLoop()

and call it from your main program:

import inc.wxgrid
inc.wxgrid.main()

if you have trouble sorting out the imports, this might help:

http://docs.python.org/ref/import.html
http://www.python.org/doc/essays/packages.html
http://effbot.org/zone/import-confusion.htm

</F>
 
J

John Lenton

Hello,

In the botom of this post you will see my source code.

The problem is when i launch main.py that doesn't make anything why
?

I'm guessing you don't have an __init__.py in inc/

--
John Lenton ([email protected]) -- Random fortune:
/* now make a new head in the exact same spot */
-- Larry Wall in cons.c from the perl source code

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFB/58QgPqu395ykGsRAttQAJ9Ty+lytzr9Nlz+UZZTN3W0bCfVQACdHhIh
ULhyExDNjL491Uh++eLGyqI=
=TsBl
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top