Python Forum

Full Version: Issue while importing variable from one file to other
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Actually previously i created a GUI in which all the classes are in same file. But now what i did for each class i created one one separate file. Now i'm importing each file where ever its required.
No its not using the same serial package.
In testing page, serial port is defined as
ser = serial.Serial(COM_Port, Baud_Rate)
ser.isOpen()
In AutomationTesting_ITOM, same serial port i'm using to send the data using the command as mentioned in function data
def data_tx(self):
        element = [0x4D, 0x08, 0xFF, 0x06, 0x1A, 0x52]
        data = bytearray(element)
        ser.write(data)
        receivedata = (ser.read(7))
        data1 = binascii.b2a_hex(receivedata)
        data2 = ord(receivedata[6])
        return data2
But as ser is define in Testing.py file, when i'm trying to use same variable in AutomationTesting_ITOM its showing error
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__ return self.func(*args) File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\AutomationTesting_ITOM.py", line 186, in clicked self.txt1.insert(END, self.data_tx()) File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\AutomationTesting_ITOM.py", line 178, in data_tx ser.write(data) AttributeError: 'str' object has no attribute 'write'
I need any solution how to access the ser variable in AutomationTesting_ITOM page
whatever the filename of file containing:
ser = serial.Serial(COM_Port, Baud_Rate)
ser.isOpen()
must be instantiated in the second file like:
import otherfilename

# Then in your new class's __init__ method add (replace with real names)
of = otherfile.calssname_where_serial_defined()
self.ser = of.ser
then in all methods that access ser, use self.ser
I tried like this. In AutomationTesting_ITOM.py file
import testingpage

class AutomationTestingPage_ITOM(Tkinter.Frame):
        
    def __init__(self, child):
        Tkinter.Frame.__init__(self, child)
        auto = testingpage.TestingPage_click(self)
        self.ser = auto.ser
        self.child = child
        self.fourthpage()
It giving error as
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__ return self.func(*args) File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\testingpage.py", line 246, in auto_itom AutomationTestingPage_ITOM(self) File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\AutomationTesting_ITOM.py", line 47, in __init__ auto = testingpage.TestingPage_click(self) AttributeError: 'module' object has no attribute 'TestingPage_click'
Is i'm doing correct or wrong plz let me know
Quote:AttributeError: 'module' object has no attribute 'TestingPage_click'
line 47
in the program that calls the AutomationTesting_ITOM module:

# add to import of class that uses AutomationTestingPage_ITOM
import AutomationTesting_ITOM

# in __init__ of class that uses AutomationTestingPage_ITOM
ati = AutomationTestingPage_ITOM.AutomationTestingPage_ITOM()

# then to call method:

ati.method(attributes)
you don't make ant changes to AutomationTestingPage_ITOM.py
Thanks i tried now its working fine.. No issues
Pages: 1 2