Python Forum

Full Version: ImiportError class not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I have an application of which the file structure is as follows each file holds but one class, and that class is the name of the file:
datainput/
    __init__.py
    main.py
    settings.py
    gui/
        __init__.py
        datainput.py
        inputwidget.py
        setupwidget.py
        ui/
            mainwindow.ui
            inputwidget.ui
            setupwidget.ui
at the beginning of each of the files in gui directory, there is:
from PuyQt4 import QtGui
import datainput.gui
and in the mainwindow
each of the subwindows are imported as well as the main gui directory.

From the setupwindow, I get user input data and I am trying to some of it to the label widgets in the mainwindow but I get the following error:
NameError: name 'DataInput' is not defined
Implying that the module has not been imported. Do I have to import it again for it to see the class DataInput? would this not then create a circular import issue?

Thinking further, should I really be creating a separate class in the main folder that will collect the data from the higher level widgets and posting them to the lower level main window using different methods?
So I have tried to create another class in the main folder, but I still can't seem to access the widgets in the ui classes. Obviously, I can't just create an instance of the class, because the data is in the window that is currently active. So when I try to access the class directly, it just spits out the error that my SetupWidget Class has no attribute 'ui_get_client' which is the name of the field in the form I have made. How do I get this data out of here so that I can then set one of the labels in the main window?
You don't show the offending code which is important.
does the module datainput.gui contain DataInput?
and if so is it properly indexed with datainput.gui.DataInput ?
Yes, I realise my issue is a little deeper than this now. Here is the code to my setup window. I am trying to keep all the offending code in one place, ideally in a seperate module eventually like a Factory Class? is that the right phrase?
from PyQt4 import QtGui

import datainput.gui

import couchdb
import time

class SetupWidget(QtGui.QWidget):
    """ Creates the client setup form for the main run sheet. """

    def __init__( self, parent = None, path = ''):
        super(SetupWidget, self).__init__(parent)

        # load the ui
        datainput.gui.loadUi( __file__, self )
        self.move(1210, 250)

        # make connections
        self.ui_ok_act.clicked.connect( self.buttonClicked )

    def buttonClicked( self ):
        server = couchdb.Server("http://192.168.1.10:5984/")
        db = server["database"]
         
        sender = self.sender()
         
        doc = {}
        doc['client'] = self.ui_get_client.text()
         
        doc['driver_coach'] = self.ui_get_dc.text()
        doc['engineer'] = self.ui_get_eng.text()
        doc['tech1'] = self.ui_get_tech1.text()
        doc['tech2'] = self.ui_get_tech2.text()
        doc['vin'] = self.ui_get_vin.text()
        doc['start_odo'] = self.ui_get_odo.text()

        self.datainput = DataInput()
        self.datainput.ui_set_client = doc['client']
         
        event_id = "Alderan"
        client_id = doc["client"]
        timestamp = time.strftime("%d-%m-%Y")    # Readable local datestamp
        doc["_id"] = event_id.replace(" ", "") + client_id.replace(" ", "")
        doc["event"] = event_id
        doc["datestamp"] = timestamp
        doc["form"] = event_id + "_setup"    # Setup form identifier
        db.save(doc)
         
        print("\nEvent data for", client_id)
        for i in doc:
            print(i, "\t", doc[i])
I suspect it is signals and slots I should be using as I could not work out how you would affect a class that was effectively opened and paused.
So after more research, I think that ultimately this is down to the way I have structured my classes. It seems that I am in the wrong class manipulating the data from the wrong directions. I don't have an answer as yet, but this thread clears up why I am having the problem that I am as I have created a circular import thing going on. This thread has helped me understand it. Unfortunately this is the only help I can give anyone else who finds this thread.

http://stackoverflow.com/questions/16821...her-module